问题描述:

I implemented example (chapter 2) from "Mastering Qt 5" book but the code crashes when adding widget to centralWidget's layout:

ui->centralWidget->layout()->addWidget(&mCpuWidget)

I suspect that the centralWidget does not have layout, hence it crashes but I don't know how to fix that?

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow),

mCpuWidget(this)

{

ui->setupUi(this);

SysInfo::instance().init();

ui->centralWidget->layout()->addWidget(&mCpuWidget);

}

Here are two more classes which might help figure out the problem. Some of you might have the book too with all of the code (hence I mentioned it).

CpuWidget::CpuWidget(QWidget* parent):

SysInfoWidget(parent),

mSeries (new QPieSeries (this))

{

mSeries->setHoleSize(0.35);

mSeries->append("CPU Load", 30.0);

mSeries->append("CPU Free", 70.0);

QChart* chart = chartView().chart();

chart->addSeries(mSeries);

chart->setTitle("CPU Average Load");

}

This class creates and sets layout (QVBoxLayout)

 SysInfoWidget::SysInfoWidget(QWidget *parent, int startDelayMs, int updateSeriesDelayMs) :

QWidget(parent),

mChartView(this)

{

mRefreshTimer.setInterval(updateSeriesDelayMs);

connect(&mRefreshTimer, &QTimer::timeout,

this, &SysInfoWidget::updateSeries);

QTimer::singleShot(startDelayMs,

[this] {mRefreshTimer.start();});

mChartView.setRenderHint(QPainter::Antialiasing);

mChartView.chart()->legend()->setVisible(false);

QVBoxLayout* layout = new QVBoxLayout(this);

layout->addWidget(&mChartView);

setLayout(layout);

}

网友答案:

Unless I have missed something in the code you have provided you haven't actually set your central widget. By default calling QMainWindow::centralWidget() returns a NULL pointer. You need to first call QMainWindow::setCentralWidget(QWidget* yourCentralWidget) before you call it. And yes, you also need to add a layout to it if you want to use layout()->addWidget(...). You can create an instance of a generic QWidget, set its layout, set is a central widget and then work with it.

网友答案:

You can fix your problem either by adding a layout in C++:

ui->setupUi(this);
SysInfo::instance().init();
ui->centralWidget->setLayout(new QVBoxLayout()); // Or any other layout class
ui->centralWidget->layout()->addWidget(&mCpuWidget);

Or in the UI Designer by using those buttons:

Note that for the buttons to be active you need to have at least 1 widget in your central widget and then select your central widget. You can then write:

ui->setupUi(this);
SysInfo::instance().init();
// One way
ui->centralWidget->layout()->addWidget(&mCpuWidget);
// Another way
ui->layout->addWidget(&mCpuWidget);

Finally you could also move your CpuWidget to the ui file using the "Promote to..." option in the contextual menu. In this case you would not need mCpuWidget but you could access it using something like ui->cpuWidget.

For more info read the Qt Designer manual:

  • Using Layouts in Qt Designer
  • Using Custom Widgets with Qt Designer
相关阅读:
Top