继承自定义选项卡栏和调整大小事件的实现
Inherit custom TabBar and implementation of resizeEvent
我正在通过QSpinBox动态插入和删除选项卡,效果很好。要填充屏幕的整个宽度(800px),我需要使用自己的 eventFilter 展开选项卡:
主窗口.h
namespace Ui {
class MainWindow;
}
class CustomTabBar : public QTabBar
{
public:
CustomTabBar(QWidget *parent = Q_NULLPTR)
: QTabBar(parent)
{
}
void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE
{
/* Resize handler */
if (e->type() == QEvent::Resize) {
// The width of each tab is the width of the tab widget / # of tabs.
resize(size().width()/count(), size().height());
}
}
void tabInserted(int index) Q_DECL_OVERRIDE
{
/* New tab handler */
insertTab(count(), QIcon(QString("")), QString::number(index));
}
void tabRemoved(int index) Q_DECL_OVERRIDE
{
/* Tab removed handler */
removeTab(count() - index);
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
CustomTabBar *tabs;
};
我的主窗口的相关代码如下所示:
主窗口.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cells = new CustomTabBar(this);
cells->tabInserted(1);
// cells->installEventFilter(resizeEvent());
}
void MainWindow::changeCells(int value) // Called when QSpinBox is changed
{
if (cells->count() < value) {
cells->tabInserted(1);
}
else if (cells->count() > value) {
cells->tabRemoved(1);
}
}
如前所述,最大宽度设置为 800 像素。所需的行为是:
- 一个标签:800px 宽度
- 两个选项卡:每个选项卡宽度 400 像素
- 。
但是无论我在哪里使用这些自定义事件之一,它都会出现段错误。
我在这里做错了什么?
根据评论,我试图覆盖 QTabBar 的 tabSizeHint 成员和"它对我有用"......
class tab_bar: public QTabBar {
protected:
virtual QSize tabSizeHint (int index) const override
{
QSize s(QTabBar::tabSizeHint(index));
s.setWidth(width() / count());
return(s);
}
};
它似乎可以根据需要工作 - 选项卡的大小可以占据整个窗口宽度。
请注意,可能还有其他样式因素会影响这一点,但我还没有时间检查。
disconnect(this, SIGNAL(resize()), this, SLOT(resizeEvent())
,然后在调用resize()
后重新connect([...])
,以避免由resize -> resizeEvent的无限递归引起的堆栈溢出。编辑:就像@G.M.说的那样。
void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE
{
disconnect(this, SIGNAL(resize()), this, SLOT(resizeEvent());
/* Resize handler */
if (e->type() == QEvent::Resize) {
// The width of each tab is the width of the tab widget / # of tabs.
resize(size().width()/count(), size().height());
}
connect(this, SIGNAL(resize()), this, SLOT(resizeEvent());
}
通过适当的设计思想,可以避免这样的解决方法,但为了给出更有用的建议,我们需要更好地了解您的用例。
由于这篇文章与这篇文章有关,我可以看到您不了解继承的工作原理。
resizeEvent
,tabInserted
和tabRemoved
是从Qt框架调用的,不应该直接调用,因此protected
。这就是insertTab
和removeTab
的目的。
你正在做的是,调用tabInserted
,它调用insertTab
,调用tabInserted
,调用insertTab
,哪个......
您要做的是在tabInserted
和tabRemoved
中实现选项卡大小调整,就像您在resizeEvent
中(有点错误)所做的那样。
if (e->type() == QEvent::Resize)
在resizeEvent
中总是正确的,因为这是Qt调用它的条件。
- Qt:模拟来自顶部父小部件的 QMouseButtonPress 事件
- QSlider 事件更改最小值、最大值或范围
- Qt. 忽略小部件中的任何鼠标事件,而不是另一个小部件
- 处理自定义QT小部件中的输入事件
- QT事件当小部件视线视线时
- 等待事件循环为空 /等到QT5小部件关闭
- 许多小部件似乎对太空栏键按事件具有默认行为.我该如何覆盖它而不将每个小部件分类
- 递归禁用子小组件鼠标滚轮事件
- WASAPI:以最小的延迟播放正弦波声音,无毛刺(专用事件驱动模式)
- 什么事件用于最大化/最小化
- 如何从类外监听小部件的调整大小事件
- 当光标不在窗口小部件上时,QT小部件检测鼠标事件
- QT 事件仅对部分小部件透明
- 为什么接收事件的多个小部件需要布局
- 从Wt小部件信号事件运行popen()时出现问题
- 使用Qt,如何使用鼠标向上或向左移动事件来调整小部件的大小
- 筛选器最小化事件
- 小部件绘制中的剪切事件
- 在internet explorer上获取最小化和最大化事件
- 如何将密钥和鼠标事件发送到 Java 小程序