简体   繁体   中英

Widgets not shown in QT main window

I'm building a simple application, the main window have to shows two widgets (QTreeView on the right, QTabWidget on the left), to perform this i use a QHBoxLayout. This is the code i've written(constructor of 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);
}

But when i run the application the main window shows no widgets. Why?

On request, i add some code:

Once a button in the mainwindows's toolbar is clicked a new tab is added to 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*)));
}

My 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();
}

I suppose your class specializes QMainWindow . If so, it needs a centralWidget to be set:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM