gpt4 book ai didi

c++ - QT 主窗口中未显示的小部件

转载 作者:行者123 更新时间:2023-11-28 06:45:44 26 4
gpt4 key购买 nike

我正在构建一个简单的应用程序,主窗口必须显示两个小部件(右侧的 QTreeView,左侧的 QTabWidget),我使用 QHBoxLayout 来执行此操作。这是我写的代码(MainWindow 的构造函数):

MainWindow::MainWindow()
{
mainLayout = new QHBoxLayout(this);
tabber = new QTabWidget(this);
analysisTreeView = new QTreeView(this);

tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);

createActions();
createMenus();
createToolBars();

connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));

setLayout(mainLayout);
}

但是当我运行应用程序时,主窗口不显示任何小部件。为什么?

根据要求,我添加了一些代码:

单击主窗口工具栏中的按钮后,将向 tabber 添加一个新选项卡:

void MainWindow::newSheet()
{
GraphicsScene *newScene = new GraphicsScene(itemMenu,this);
QGraphicsView *newView = new QGraphicsView(this);
newScene->setSceneRect(-200, -200, 400, 400);
newView->scale(1.5,1.5);
newView->setCacheMode(QGraphicsView::CacheBackground);
newView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
newView->setRenderHint(QPainter::Antialiasing);
newView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
newView->setScene(newScene);
sheetList.append(newView);
tabber->addTab(newView,"PNC");
connect(newScene, SIGNAL(itemInserted(PItem*)), this, SLOT(itemInserted(PItem*)));
connect(newScene, SIGNAL(requestUpdateGUI(GraphicsScene*)), this, SLOT(updateGUI(GraphicsScene*)));
}

我的 main.cpp:

#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(application);

QApplication a(argc, argv);
MainWindow window;

window.showMaximized();
return a.exec();
}

最佳答案

我想你的类(class)专门研究QMainWindow。如果是这样,它需要设置一个 centralWidget:

MainWindow::MainWindow()
{
// added by jpo38
QWidget* mainWidget = new QWidget( this );
setCentralWidget( mainWidget );
// end added by jpo38

mainLayout = new QHBoxLayout(mainWidget);
tabber = new QTabWidget(mainWidget);
analysisTreeView = new QTreeView(mainWidget);

tabber->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

mainLayout->addWidget(tabber, 0);
mainLayout->addWidget(analysisTreeView, 0);

createActions();
createMenus();
createToolBars();

connect(tabber, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));

setLayout(mainLayout);
}

关于c++ - QT 主窗口中未显示的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25022054/

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