C++ 将矩形 Qt 按钮更改为圆形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12734319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:34:01  来源:igfitidea点击:

Change rectangular Qt button to round

c++qtqt-designer

提问by DarwinIcesurfer

I'm trying to create a round button in Qt. A simple form with a single button QPushButtonwas created in designer. I'm attempting to turn this into a round button using setMask(). As soon as setMask()is applied the button disappeares. Does a custom widget need to be created to make a round button?

我正在尝试在 Qt 中创建一个圆形按钮。QPushButton在设计器中创建了一个带有单个按钮的简单表单。我正在尝试使用setMask(). 一旦setMask()应用,按钮就会消失。是否需要创建自定义小部件来制作圆形按钮?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QtGui/QPushButton>


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

    ui->pushButton->setText("Test Text");
    ui->pushButton->setFixedHeight(200);
    ui->pushButton->setFixedWidth(200);

    //Set Starting point of region 5 pixels inside , make region width & height
    //values same and less than button size so that we obtain a pure-round shape

    QRegion* region = new QRegion(*(new QRect(ui->pushButton->x()+5,ui->pushButton->y()+5,190,190)),QRegion::Ellipse);
    ui->pushButton->setMask(*region);
    ui->pushButton->show();


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QMessageBox msgbox;
    msgbox.setText("Text was set");
    msgbox.show();

}

Note: If the button is created in code and applied to a window before the window is displayed, the button is displayed. I would like to use the WYSIWIG capabilities of the Qt Designer rather than creating the entire form in code.

注意:如果按钮是在代码中创建并在窗口显示之前应用于窗口,则显示该按钮。我想使用 Qt Designer 的 WYSIWIG 功能,而不是在代码中创建整个表单。

回答by stackunderflow

It is going invisible, but its because you do not have the ellipse centered around the correct point.

它变得不可见,但这是因为您没有以正确的点为中心的椭圆。

QWidget::setMask"causes only the parts of the widget which overlap region to be visible. If the region includes pixels outside the rect() of the widget, window system controls in that area may or may not be visible, depending on the platform".

QWidget::setMask"导致只有重叠区域的小部件部分可见。如果该区域包含小部件的 rect() 之外的像素,则该区域中的窗口系统控件可能会也可能不可见,具体取决于平台”。

Try this code instead and you'll see:

试试这个代码,你会看到:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton->setText("Test Text");
    ui->pushButton->setFixedHeight(200);
    ui->pushButton->setFixedWidth(200);
    QRect rect(0,0,190,190);
    qDebug() << rect.size();
    qDebug() << ui->pushButton->size();
    QRegion region(rect, QRegion::Ellipse);
    qDebug() << region.boundingRect().size();
    ui->pushButton->setMask(region);
}

Ps. Why do you set the height of the pushButton twice? I'm assuming that's a typo and you meant width.

附言。为什么要两次设置按钮的高度?我假设这是一个错字,你的意思是宽度。

回答by Lol4t0

I think the simplest solution would be using stylesheet.

我认为最简单的解决方案是使用样式表。

Like this:

像这样:

 background-color: white;
 border-style: solid;
 border-width:1px;
 border-radius:50px;
 border-color: red;
 max-width:100px;
 max-height:100px;
 min-width:100px;
 min-height:100px;

See also examplesand reference.

另请参阅示例参考

Note, that you have to create complete style for your button, as standard style will not be applicable.

请注意,您必须为按钮创建完整的样式,因为标准样式将不适用。