繁体   English   中英

如何从班级中的插槽调整主窗口的大小?

[英]How can i resize main window from slot in my class?

在构造函数中很容易,但是如何在插槽中调整大小? 在构造函数中我只是这样做:

MyDialogWindow::MyDialogWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyDialogWindow)
{ 
.....
    int x=this->width()*1;
    int y=this->height()*0.5;
    this->setFixedSize(x,y);
....
}

但同样在插槽中不起作用:

void MyDialogWindow::on_pushButton_clicked()
{
int x=this->width()*1;
int y=this->height()*1;
this->setFixedSize(x,y);

}

看来我在方法中没有主窗口的对象,那怎么一定呢?

你需要调用this->updateGeometry()setFixedSize信号窗口小部件的重新布局:

void MyDialogWindow::on_pushButton_clicked()
{
    int x=this->width()*1;
    int y=this->height()*1;//make sure something actually changes with the size though
    this->setFixedSize(x,y);
    this->updateGeometry();
}

尝试使用QWidget :: setGeometry

void MyDialogWindow::on_pushButton_clicked()
{
    int x = this->width()  * 2;
    int y = this->height() * 2;
    this->setFixedSize( x , y );
    this->updateGeometry( );
}

这应该工作,因为this->width( ) * 1总是相同的大小。 你不会看到差异。

最好在你的类中使xy全局化并在那里设置宽度和高度,只需在单击按钮时指定新的大小,这样每次单击按钮时都不会增加它:

int x = ui->pushButton.width()  * 2;
int y = ui->pushButton.height() * 2;

void MyDialogWindow::on_pushButton_clicked()
{
    this->setFixedSize( x , y );
    this->updateGeometry( );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM