QLabel and QPushButton align

后端 未结 1 980
谎友^
谎友^ 2021-01-25 08:16

I\'m having a hard time aligning multiple qt widgets (label and push button). I want the widgets (colored green and red respectively) to line up. Any advice?

#i         


        
相关标签:
1条回答
  • 2021-01-25 09:11

    Using QVBoxLayout would order the widgets vertically. To line up 2 widgets horizontally, QFormLayout can be used:

    #include <QLabel>
    #include <QPushButton>
    #include <QFormLayout>
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        QFormLayout * const formlayout = new QFormLayout(ui->scrollAreaWidgetContents);
        for(int i=0; i!=100; ++i)
        {
          QLabel *label = new QLabel();
          label->setText(QString::number(i));
          label->setStyleSheet("background-color: red");
          label->setFixedWidth(100);
          QPushButton *pushButton = new QPushButton();
          int menu_x_pos = label->pos().x();
          int menu_y_pos = label->pos().y();
          pushButton->setGeometry(menu_x_pos+120, menu_y_pos,10,20);
          pushButton->setText(QString::number(i));
          pushButton->setStyleSheet("background-color: green");
          formlayout->insertRow(i,label,pushButton);
        }
    }
    

    This is how it would look like:

    Now for more widgets in a row, its slightly different, you could use QHBoxLayout layout along with the form layout, put all widgets to be lined up in a single horizontal layout, then that layout is added as a row to your form layout:

    #include <QLabel>
    #include <QPushButton>
    #include <QFormLayout>
    #include <QCheckBox>
    #include <QHBoxLayout>
    #include <QLineEdit>
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        QFormLayout * const formlayout = new QFormLayout(ui->scrollAreaWidgetContents);
        QHBoxLayout* hlayout[100];
        QLineEdit* lineEdit[100];
        for(int i=0; i!=100; ++i)
        {
          hlayout[i] = new QHBoxLayout();
          QLabel *label = new QLabel();
          label->setText(QString::number(i));
          label->setStyleSheet("background-color: red");
          label->setFixedWidth(100);
          hlayout[i]->addWidget(label);
          //
          QPushButton *pushButton = new QPushButton();
          int menu_x_pos = label->pos().x();
          int menu_y_pos = label->pos().y();
          pushButton->setGeometry(menu_x_pos+120, menu_y_pos,10,20);
          pushButton->setText(QString::number(i));
          pushButton->setStyleSheet("background-color: green");
          hlayout[i]->addWidget(pushButton);
          //
          QCheckBox *checkbox = new QCheckBox();
          checkbox->setText("CheckB:");
          hlayout[i]->addWidget(checkbox);
          //
          lineEdit[i] = new QLineEdit;
          lineEdit[i]->setText("Text");
          hlayout[i]->addWidget(lineEdit[i]);
    // now line up all widgets in a row
          formlayout->insertRow(i,hlayout[i]);
        }
    }
    

    0 讨论(0)
提交回复
热议问题