CentralWidget adjust size to show all the content
我是Qt的初学者,我还不了解centralWidget的布局。
我有一个要添加到中央Widget的自定义QWidget子类(带有.ui和cpp类)。
我想了解如何在每次添加内容时对QMainWindow子类说以调整大小和适合内容。
我已经在mainwindow和centralWidget对象上尝试了
无论如何,我是以这种方式添加小部件的:
1 2 3 4 5 6 7 8 9 | MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); MyWidget *w = new Undistort(); w->setParent(this->centralWidget()); } |
一些忠告?
给定示例,根据Pixmap的大小,QMainWindow将调整大小。 通常,这不是理想的情况,因为用户MainWindow需要在桌面上显示,它不应大于您的桌面屏幕大小。 我不确定您是否真的在寻找这个。 从SO Ans复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include"mainwindow.h" #include <QLabel> #include <QHBoxLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass) { ui->setupUi(this); QPixmap pix; pix.load("C:\\\\Users\\\\user\\\\Desktop\\\\Uninstallation failure2.png"); //Replace with ImageLabel2 QLabel* image = new QLabel(this); image->setPixmap(pix); QHBoxLayout* hbox = new QHBoxLayout(this); hbox->addWidget(image); QWidget* centreWidget = new QWidget(); //QMainwindow, having a feature called centreWidget, to set the layout. centreWidget->setLayout( hbox ); setCentralWidget( centreWidget ); } |