QGridLayout和QPushButton或网格中的圆圈

时间:2018-03-01 10:15:37

标签: c++ qt geometry

我想在网格的最后一列中有一些小圆圈。如果可能的话,我不想插入图像并创建圆圈。通过转换或使按钮完全圆形,或者单选按钮或其他方法可能更好。我想让圆圈大小比当前圆圈大。有什么建议?谢谢enter image description here

 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 #include <QLineEdit>
 #include <QPushButton>
 #include <QGridLayout>
 #include <QLabel>
 #include <QRadioButton>

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

  ui->scrollArea->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
    ui->scrollArea->setWidgetResizable( true );

    QWidget *widget = new QWidget();
    ui->scrollArea->setWidget( widget );

    QGridLayout *layout = new QGridLayout();
    widget->setLayout( layout );

    for (int i = 0; i < 20; i++)
    {
        QLabel *label = new QLabel( QString( "%1" ).arg( i ) );
        layout->addWidget(label,i,1,1,1 );
        layout->setColumnMinimumWidth(1, 100);
        layout->setColumnMinimumWidth(2, 10);    

        if (i==5)
        {
            QLineEdit *lineEdit = new QLineEdit;
            layout->addWidget(lineEdit,i,5,1,1);
           // layout->setColumnMinimumWidth(4, 100);

        } else
        {
        QLineEdit *lineEdit_A = new QLineEdit;
        layout->addWidget(lineEdit_A,i,3,1,1);
        layout->setColumnMinimumWidth(4, 25);
        layout->setColumnMinimumWidth(5, 50);
        layout->setColumnMinimumWidth(6, 25);

      //            layout->setColumnStretch(3,10);
        QLineEdit *lineEdit_B = new QLineEdit;
        layout->addWidget(lineEdit_B,i,7,1,1);
        layout->setColumnMinimumWidth(8, 10);
        }

        //layout->setColumnMinimumWidth(12, 100);   
        //layout->setColumnMinimumWidth(13, 100);    


        layout->setColumnMinimumWidth(9, 10);
        QPushButton *button = new QPushButton;


        layout->addWidget(button,i,10,1,1);
        button->setStyleSheet(
                     "border-color: green;"
                     "border-width: 2px;"
                     "border-style: solid;"
                     "border-radius: 7px;"
                     "margin:1px;"
                     "padding:1px;");



        layout->setColumnMinimumWidth(10, 50);
        layout->setColumnMinimumWidth(11, 10);

      }

     }

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

1 个答案:

答案 0 :(得分:0)

我找到了以下代码来为我的应用程序构建一个圆形按钮:

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

完美无缺。另外我可以改变背景颜色。谢谢你的帮助。