gpt4 book ai didi

c++ - 继承自定义TabBar和resizeEvent的实现

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:14 26 4
gpt4 key购买 nike

我通过 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);
}
};

它似乎可以按您的要求工作——选项卡的大小可以占据整个窗口宽度。

请注意,可能还有其他样式因素会对此产生影响,但我还没有时间检查。

关于c++ - 继承自定义TabBar和resizeEvent的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37719340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com