Derrived Widget not centered on parent when shown as dialog

后端 未结 2 396
栀梦
栀梦 2021-01-24 08:54

I have a class MyListWidget derrived from QWidget. I passed parent and flags to the base class QWidget constructor (tried both Qt::Dialog and Qt::Popup in tests) but the custom

相关标签:
2条回答
  • 2021-01-24 09:06

    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()
        );
    }
    

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

    0 讨论(0)
  • 2021-01-24 09:20

    I'm not quite sure what you're trying to achieve but I have the feeling you should derive MyListWidget from QDialog.

    Regards,

    Ben

    0 讨论(0)
提交回复
热议问题