如何将QProgressDialog中的取消按钮移动到中心?

huangapple go评论131阅读模式
英文:

How to move cancel button in QProgressDialog to the center?

问题

Sure, here's the translation of your code-related request:

我想将 `QProgressDialog` 的取消按钮移动到中间;默认情况下它位于左侧。

以下是一个简单的示例,但我不知道如何更改按钮位置。

_progress = new QProgressDialog();
_progress->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
_progress->setWindowTitle(tr("Stimulated Movement"));
_progress->setLabelText(tr("请等待一会儿,电机正在移动!"));
_progress->setAttribute(Qt::WA_DeleteOnClose);
_progress->setModal(true);
// 让它保持滚动
_progress->setRange(0, 0);
_progress->setCancelButton(new QPushButton(tr("终止")));
英文:

I want to move the Cancel button of QProgressDialog to the center; by default its on the left.

Below is a simple example, but I don't know how to change the button position.

_progress = new QProgressDialog();
_progress->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
_progress->setWindowTitle(tr("Stimulated Movement"));
_progress->setLabelText(tr("Please wait for a moment while the motor is moving!"));
_progress->setAttribute(Qt::WA_DeleteOnClose);
_progress->setModal(true);
//make it Keep rolling
_progress->setRange(0, 0);
_progress->setCancelButton(new QPushButton(tr("Terminate")));

答案1

得分: 1

Remy Lebeau说:
> 你可以在对话框显示时将QPushButton移动到所需位置,你有访问取消按钮的权限,是什么阻止了你这样做?

以下是如何实现的示例代码:

QProgressDialog *progress = new QProgressDialog();
progress->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
progress->setWindowTitle("模拟运动");
progress->setLabelText("请稍等片刻,电机正在运动!");
progress->setAttribute(Qt::WA_DeleteOnClose);
progress->setModal(true);
progress->setRange(0, 0);
QPushButton *b = new QPushButton("终止");
progress->setCancelButton(b);

progress->show();
//在显示后将按钮移动到进度对话框的中心
//y坐标保持不变,需要根据按钮和对话框宽度计算x坐标
b->move((progress->width() - b->width()) / 2, b->geometry().y());

然而,这种方法存在一个问题,如果调整对话框的大小,按钮将不会保持在中心位置。

要解决这个问题,需要重新实现resizeEvent,但如果你不需要调整大小,可以考虑为对话框设置固定大小来防止这种情况发生。

例如:

progress->setFixedSize(310, 100);
英文:

Remy Lebeau said:
>You have access to the QPushButton for the cancel, what's stopping you from simply moving it where you want when the dialog is shown?

Here's how you can implement that:

QProgressDialog *progress = new QProgressDialog();
progress->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
progress->setWindowTitle("Stimulated Movement");
progress->setLabelText("Please wait for a moment while the motor is moving!");
progress->setAttribute(Qt::WA_DeleteOnClose);
progress->setModal(true);
progress->setRange(0, 0);
QPushButton *b = new QPushButton("Terminate");
progress->setCancelButton(b);

progress->show();
//move it to the center of progress after you show it
//y coordinate stays the same, and you have to calculate x relatively to the button and dialog width 
b->move((progress->width()-b->width())/2,b->geometry().y());

However there is a problem with this method, the button will not stay in the center if the dialog is resized.

Fixing that, needs resizeEvent reimplementation, but if you don't need resizing, you might just want to set a fixed size for the dialog to prevent it.

For example:

progress->setFixedSize(310,100);

huangapple
  • 本文由 发表于 2023年5月10日 23:57:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定