Qt widgets do not appear in the main window

I am trying to create a main application window. It is assumed that it has a menu bar and shows one label with a separator at the bottom. My code for the main window is below.

The problem is that only the shortcut and the separator are displayed in the menu bar. Things I've tried so far: 1. Set the parent delimiter to "this" β†’ the splitter, but is drawn above the menu bar and is small. 2. Set the parent label to "this" β†’, and then draw over the menu bar. 3. Set the parent to "this" for the separator, the parent and the shortcut β†’ the label is not displayed, I get a miniature version of the slipper drawn under the menu bar. 4. I tried dragging and dropping lines of code and got various other results, such as a large separator drawn above the menu bar that does not resize using the window, a small separator under the menu bar, just a menu bar and nothing else, etc. - nothing useful.

It seems that the vertical layout is completely ignored.

I don’t know 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(); 
+4
source share
2 answers

You need to install the central widget by calling setCentralWidget () .

EDIT: Add a QWidget to the main window, set it as the center widget, create a layout, and finally add it to the center 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); } 
+1
source

I had exactly the same problem. I don't know what caused this, but inheriting from QWidget instead of QMainWindow seems to fix it.

+1
source

Source: https://habr.com/ru/post/1402580/


All Articles