Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QGridLayout和QPushButton或网格中的圆圈_C++_Qt_Geometry - Fatal编程技术网

C++ QGridLayout和QPushButton或网格中的圆圈

C++ QGridLayout和QPushButton或网格中的圆圈,c++,qt,geometry,C++,Qt,Geometry,我想在网格的最后一列有一个小圆圈。如果可能的话,我不希望插入图像并创建圆。通过转换或使按钮完全圆形,或者单选按钮或其他方法将更可取。我想要一个和现在一样大或更小的圆。有什么建议吗?多谢各位 #包括“mainwindow.h” #包括“ui_main window.h” #包括 #包括 #包括 #包括 #包括 主窗口::主窗口(QWidget*父窗口): QMainWindow(父级), 用户界面(新用户界面::主窗口) { 用户界面->设置用户界面(此); ui->scrollArea->set

我想在网格的最后一列有一个小圆圈。如果可能的话,我不希望插入图像并创建圆。通过转换或使按钮完全圆形,或者单选按钮或其他方法将更可取。我想要一个和现在一样大或更小的圆。有什么建议吗?多谢各位

#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
#包括
#包括
#包括
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
ui->scrollArea->SetWidgetResizeable(真);
QWidget*widget=新的QWidget();
ui->scrollArea->setWidget(小部件);
QGridLayout*布局=新的QGridLayout();
小部件->设置布局(布局);
对于(int i=0;i<20;i++)
{
QLabel*label=新的QLabel(QString(“%1”).arg(i));
布局->添加小部件(标签,i,1,1,1);
布局->设置列最小宽度(1100);
布局->设置列最小宽度(2,10);
如果(i==5)
{
QLineEdit*lineEdit=新的QLineEdit;
布局->添加小部件(lineEdit,i,5,1,1);
//布局->设置列最小宽度(4100);
}否则
{
QLineEdit*lineEdit_A=新的QLineEdit;
布局->添加小部件(lineEdit_A,i,3,1,1);
布局->设置列最小宽度(4,25);
布局->设置柱最小宽度(5,50);
布局->设置柱最小宽度(6,25);
//布局->设置柱拉伸(3,10);
QLineEdit*lineEdit\u B=新的QLineEdit;
布局->添加小部件(lineEdit_B,i,7,1,1);
布局->设置柱最小宽度(8,10);
}
//布局->设置列最小宽度(12100);
//布局->设置列最小宽度(13100);
布局->设置列最小宽度(9,10);
QPushButton*按钮=新的QPushButton;
布局->添加小部件(按钮,i,10,1,1);
按钮->设置样式表(
“边框颜色:绿色;”
“边框宽度:2px;”
“边框样式:实心;”
“边界半径:7px;”
“保证金:1px;”
“填充:1px;”;
布局->设置柱最小宽度(10,50);
布局->设置柱最小宽度(11,10);
}
}
MainWindow::~MainWindow()
{
删除用户界面;
}

我发现以下代码可以为我的应用程序构建圆形按钮:

QPushButton *button = new QPushButton;
layout->addWidget(button,i,10,1,1);

button->setFixedHeight(30);
button->setFixedWidth(30);
button->setStyleSheet("background-color:green;");

//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(QRect(button->x()+5,button->y()+5,20,20), QRegion::Ellipse);
button->setMask(region);

它工作得很好。另外,我可以改变背景颜色。感谢您的帮助。

在按钮样式中,设置相对于宽度和高度的半径,请参见第二个答案,了解此圆圈的用途?它应该是可点击的吗?它应该像一个复选框一样工作吗?不适合将QRegion创建为指针,您必须使用:
QRegion区域(QRect(button->x()+5,button->y()+5,20,20),QRegion::eliple);按钮->设置掩码(区域)是,除非您有充分的理由这样做,否则不要使用
new
分配变量。
QPushButton *button = new QPushButton;
layout->addWidget(button,i,10,1,1);

button->setFixedHeight(30);
button->setFixedWidth(30);
button->setStyleSheet("background-color:green;");

//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(QRect(button->x()+5,button->y()+5,20,20), QRegion::Ellipse);
button->setMask(region);