且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当显示为对话框时,Derrived Widget不在父对象的中心

更新时间:2023-11-30 22:24:10

默认情况下不会在中心显示QWidget ,所以你需要手动集中它(你可以在构造函数中)

QWidget is not shown on center by default, so you need to center it manually (you can do that in the constructor):

MyListWidget::MyListWidget(QWidget* parent, Qt::WindowFlags flags)
    : QWidget(parent, flags),
      ui(std::auto_ptr<Ui::MyListWidget>(new Ui::MyListWidget))
{
    ui->setupUi(this);
    move(
       parent->window()->frameGeometry().topLeft() +
       parent->window()->rect().center() - rect().center()
    );
}

注意 std :: auto_ptr ,这些天你可能想使用 std :: unique_ptr

P.S. Beware of std::auto_ptr, you probably want to use std::unique_ptr these days.