我正在尝试动态地在MainWindow类中创建布局。我有四个用网格布局对象放置的框架。每个帧包含一个自定义ClockWidget。我想在调整主窗口大小时相应地调整ClockWidget对象的大小,所以我需要将它们添加到布局中。但是,我需要在运行时执行此操作,因为对象本身是在运行时创建的。我尝试以编程方式完成此操作,但下面注释掉的代码尝试创建新布局会导致程序崩溃。正确执行此操作的步骤是什么?
标题文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ClockView.h"
namespace Ui{
class MainWindow;
}
class QLayout;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void populateViewGrid();
private:
Ui::MainWindow *ui;
ClockView *clockView_1;
ClockView *clockView_2;
ClockView *clockView_3;
ClockView *clockView_4;
QLayout *layout_1;
QLayout *layout_2;
QLayout *layout_3;
QLayout *layout_4;
};
#endif // MAINWINDOW_H
实施档案:
#include <QVBoxLayout>
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
populateViewGrid();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::populateViewGrid()
{
clockView_1 = new ClockView(ui->frame_1);
clockView_2 = new ClockView(ui->frame_2);
clockView_3 = new ClockView(ui->frame_3);
clockView_4 = new ClockView(ui->frame_4);
/*
layout_1 = new QVBoxLayout;
layout_2 = new QVBoxLayout;
layout_3 = new QVBoxLayout;
layout_4 = new QVBoxLayout;
layout1->addWidget(clockView_1);
layout2->addWidget(clockView_2);
layout3->addWidget(clockView_3);
layout4->addWidget(clockView_4);
ui->frame_1->setLayout(layout_1);
ui->frame_2->setLayout(layout_2);
ui->frame_3->setLayout(layout_3);
ui->frame_3->setLayout(layout_4);
*/
}
答案 0 :(得分:6)
您的程序是正确的。有一些拼写错误,例如,您要为frame3
设置两次布局。这可能是你的问题。崩溃并不总是可重现的。我不认为你有任何其他问题。以下是一个自包含的示例。它还通过值保存所有实例,避免通过指针过度消除额外的解引用。
// https://github.com/KubaO/stackoverflown/tree/master/questions/dynamic-widget-10790454
#include <cmath>
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
#include <array>
// Interface
class ClockView : public QLabel
{
public:
explicit ClockView(QWidget* parent = nullptr) : QLabel(parent)
{
static int ctr = 0;
setText(QString::number(ctr++));
}
};
class MainWindow : public QMainWindow
{
public:
explicit MainWindow(QWidget *parent = nullptr);
void populateViewGrid();
private:
static constexpr int N = 10;
QWidget central{this};
QGridLayout centralLayout{¢ral};
std::array<QFrame, N> frames;
std::array<ClockView, N> clockViews;
std::array<QVBoxLayout, N> layouts;
};
// Implementation
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setCentralWidget(¢ral);
const int n = ceil(sqrt(N));
for (int i = 0; i < N; ++ i) {
frames[i].setFrameShape(QFrame::StyledPanel);
centralLayout.addWidget(&frames[i], i/n, i%n, 1, 1);
}
populateViewGrid();
}
void MainWindow::populateViewGrid()
{
for (int i = 0; i < N; ++ i) {
layouts[i].addWidget(&clockViews[i]);
frames[i].setLayout(&layouts[i]);
}
}
int main(int argc, char** argv)
{
QApplication app{argc, argv};
MainWindow w;
w.show();
return app.exec();
}
和qmake项目文件。
greaterThan(QT_MAJOR_VERSION, 4) {
QT = widgets
CONFIG += c++11
} else {
QT = gui
unix:QMAKE_CXXFLAGS += -std=c++11
macx {
QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
}
}
TARGET = dynamic-widget-10790454
TEMPLATE = app
SOURCES += main.cpp