Qt动态创建布局并向布局添加小部件

编程入门 行业动态 更新时间:2024-10-22 05:17:02
本文介绍了Qt动态创建布局并向布局添加小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在MainWindow类中动态创建布局.我有四个用网格布局对象放置的框架.每帧都包含一个自定义的ClockWidget.我希望在调整主窗口大小时相应地调整ClockWidget对象的大小,因此需要将它们添加到布局中.但是,我需要在运行时执行此操作,因为对象本身是在运行时创建的.我尝试以编程方式完成此操作,但是下面尝试创建新布局的注释掉的代码导致程序崩溃.正确执行此操作的步骤是什么?

I am trying to create layouts in my MainWindow class dynamically. I have four frames which are laid with a grid layout object. Each frame contains a custom ClockWidget. I want the ClockWidget objects to resize accordingly when I resize the main window, so I need to add them to a layout. However, I need to do this at runtime, since the object itself is created at runtime. I tried to accomplish this programmatically, but the commented-out code below attempting to create a new layout causes the program to crash. What is the procedure for doing this correctly?

头文件:

#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); */ }

推荐答案

您的过程正确.有一些错别字,例如,您为frame3设置了两次布局.那可能是你的问题.崩溃并非总是可以重现.除此以外,我认为您没有其他问题.以下是一个自包含的示例.它还可以按值保留所有实例,避免过早地通过指针对额外的取消引用进行了悲观的预测.

Your procedure is correct. There are some typos, for example, you're setting the layout twice for frame3. That may be your problem. Crashes aren't always reproducible. I don't think you have any other problems than that. Below is a self contained example. It also keeps all the instances by value, avoiding the premature pessimization of an extra dereference via a pointer.

// github/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{&central}; std::array<QFrame, N> frames; std::array<ClockView, N> clockViews; std::array<QVBoxLayout, N> layouts; }; // Implementation MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setCentralWidget(&central); 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项目文件.

And the qmake project file.

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

更多推荐

Qt动态创建布局并向布局添加小部件

本文发布于:2023-11-11 18:12:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   并向   部件   动态   Qt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!