Galadrius Krunthar
Galadrius Krunthar

Reputation: 501

Qt widgets not showing up in main window

I am trying to create main application window. It is supposed to have a menu bar, and show one label with splitter underneath. My code for main window is below.

The problem I have is that menu bar shows but label and splitter do not. Things I tried so far: 1. Set splitter's parent to "this" -> splitter shows, but is drawn over menu bar and is small. 2. Set label's parent to "this" -> label shows, but is drawn over menu bar. 3. Set parent to "this" for splitter, parent and label ->label doesn't show, I get miniature version of slipper drawn underneath menu bar. 4. Tried reshuffling of code lines and got various other results, like big splitter drawn over menu bar that doesn't resize with window, small splitter underneath menu bar, just menu bar and nothing else, etc -- nothing useful.

Looks like vertical layout is completely ignored.

No idea what else to try. Any suggestions?

MyWindow::MyWindow(IViewSignalHandler* signalHandler, QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    m_signalHandler = signalHandler;

    // menu
    m_fileMenu = new QMenu(tr("&File"));
    m_fileMenu->addAction(tr("&Open"), this, SLOT(slot_OpenFile(bool)));

    m_helpMenu = new QMenu(tr("&Help"));
    m_helpMenu->addAction(tr("&About"), this, SLOT(slot_ShowAboutBox(bool)));

    menuBar()->addMenu(m_fileMenu);
    menuBar()->addMenu(m_helpMenu);

    // graph
    m_graphWidget = new QwtPlot();
    m_graphLegend = new QwtLegend();
    m_graphLegend->setItemMode(QwtLegend::CheckableItem);
    m_graphWidget->insertLegend(m_graphLegend, QwtPlot::RightLegend);
    m_graphWidget->setAxisTitle(QwtPlot::xBottom, tr("X"));
    m_graphWidget->setAxisScale(QwtPlot::xBottom, DEFAULT_X_MIN, DEFAULT_X_MAX);
    m_graphWidget->setAxisTitle(QwtPlot::yLeft, tr("Y"));
    m_graphWidget->setAxisScale(QwtPlot::yLeft, DEFAULT_Y_MIN, DEFAULT_Y_MAX);

    QwtPlotZoomer* zoomer = new QwtPlotZoomer(m_graphWidget->canvas());
    zoomer->setTrackerMode(QwtPlotZoomer::AlwaysOn);
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier);
    zoomer->setMousePattern(QwtEventPattern::MouseSelect3, Qt::RightButton);

    // path label
    m_label= new QLabel();
    m_label->setTextFormat(Qt::RichText);
    m_label->setWordWrap(false);
    m_label->setText(tr("<b>Label: </b>"));

    // splitter
    m_splitter = new QSplitter();
    m_splitter->setChildrenCollapsible(true);
    m_list = new QListWidget();
    m_splitter->addWidget(m_list);
    m_tree = new QTreeWidget();
    m_splitter->addWidget(m_tree);
    m_text = new QTextEdit();
    m_splitter->addWidget(m_text);
    m_splitter->addWidget(m_graphWidget);

    // page layout
    QVBoxLayout *pageLayout = new QVBoxLayout(this);
    pageLayout->addWidget(m_label);
    pageLayout->addWidget(m_splitter);
    setLayout(pageLayout);
}

[...]

m_mainWindow = new MyWindow(this);
m_mainWindow->show();

Upvotes: 3

Views: 7830

Answers (2)

Arnold Spence
Arnold Spence

Reputation: 22272

You need to set a central widget by calling setCentralWidget().

EDIT: Add a QWidget to your Main Window, set it as central widget, create your layout and finally add it to the central widget.

MyWindow::MyWindow(IViewSignalHandler* signalHandler, QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
  QWidget *ui_area = new QWidget;
  setCentralWidget(ui_area);

  //.....create your_layout.....

 ui_area->setLayout(your_layout);

}

Upvotes: 1

ShdNx
ShdNx

Reputation: 3213

I just had the exact same problem. I do not know what caused it, but inheriting from QWidget instead of QMainWindow seems to have fixed it.

Upvotes: 1

Related Questions