QLabel and QPushButton align

三世轮回 提交于 2019-12-20 04:52:42

问题


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?

#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
: QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QVBoxLayout * const layout = new QVBoxLayout(ui->scrollAreaWidgetContents);
for(int i=0; i!=100; ++i)
{
  QLabel *label = new QLabel();
  layout->addWidget(label);
  label->setText(QString::number(i));
  label->setStyleSheet("background-color: red");
  label->setFixedWidth(100);
  QPushButton *pushButton = new QPushButton();
  layout ->addWidget(pushButton);
  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");
  }
  }
  Dialog::~Dialog()
{
  delete ui;
 }


回答1:


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



来源:https://stackoverflow.com/questions/49020491/qlabel-and-qpushbutton-align

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!