2

我是 Qt 的初学者,我还不了解 centralWidget 上的布局。

我有一个要添加到中央小部件的自定义 QWidget 子类(带有 .ui 和 cpp 类)。

我想了解如何对 QMainWindow 子类说,以在我添加内容时调整大小并适应内容。

adjustSize在 mainwindow 和 centralWidget 对象上都尝试过使用方法,但没有任何改变..

无论如何,我以这种方式添加小部件:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyWidget *w = new Undistort();
    w->setParent(this->centralWidget());
}

一些忠告?

4

1 回答 1

1

给定示例,根据像素图大小,QMainWindow 将调整大小。通常这不是理想的情况,因为用户 MainWindow 需要显示在桌面上,它不应该大于您的桌面屏幕大小。我不确定你是否真的在寻找这个。复制自SO Ans

#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 );
}
于 2013-09-02T14:27:02.937 回答