How to align QLabel correctly in a QGridLayout? - c++

I am trying to align QLabels in a simple GridLayout but this doesn't work seems to be a bug in QT 5.9 ?
Here is my snippet, everything is in a QDialog:
MyDialogue::MyDialogue(QWidget *parent) : QDialog(parent) {
QLabel *labelA = new QLabel(); labelA->setFixedSize(100, 25);
QLabel *labelB = new QLabel(); labelB->setFixedSize(100, 25);
QLabel *labelC = new QLabel(); labelC->setFixedSize(100, 25);
QLabel *labelD = new QLabel(); labelD->setFixedSize(100, 25);
labelA->setStyleSheet("background-color:blue");
labelB->setStyleSheet("background-color:yellow");
labelC->setStyleSheet("background-color:purple");
labelD->setStyleSheet("background-color:green");
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(labelA, 1, 1);
layout->addWidget(labelB, 1, 2);
layout->addWidget(labelC, 2, 1, 2, 2);
layout->addWidget(labelD, 3, 1, 3, 2);
}
The result:

Ok I found the solution (my mistake) :
MyDialogue::MyDialogue(QWidget *parent) : QDialog(parent) {
QLabel *labelA = new QLabel();
QLabel *labelB = new QLabel();
QLabel *labelC = new QLabel();
QLabel *labelD = new QLabel();
labelA->setStyleSheet("background-color:blue");
labelB->setStyleSheet("background-color:yellow");
labelC->setStyleSheet("background-color:purple");
labelD->setStyleSheet("background-color:green");
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(labelA, 1, 1);
layout->addWidget(labelB, 1, 2);
layout->addWidget(labelC, 2, 1, 1, 2);
layout->addWidget(labelD, 3, 1, 1, 2);
}
I was incorrectly thinking that for expanding a row along 2 columns (the case of labelC and labelD) I had to write the corrdinates of the starting cell (2,1) the actual position and then the ending cell (2,2). I was misguided by a Java layout manager that worked exactly that way. Just for the record here you just have to indicate the total number of row span and column span which is 2 in my case.

Related

QFormLayout expands column of QGridLayout

I am creating a QFormLayout with some items like this:
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);
After that I try to add this Layout to a QGridLayout like this:
QGridLayout *layout = new QGridLayout();
QPushButton *btn1 = new QPushButton();
QPushButton *btn2 = new QPushButton();
layout->addWidget(btn, 1, 1, 3, 3);
layout->addWidget(btn2, 1, 4);
layout->addLayout(tableLayout, 2, 4);
After I added the tableLayout, btn1 as width as 1 column and the tableLayout is as width as 3 columns.
I already tried to put the QFormLayout into a own widget and add the widget to the QGridLayout. But it didn't changed anything. The way I am doing that is the following:
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);
QWidget *widget = new QWidget();
widget->setLayout(tableLayout);
QGridLayout *layout = new QGridLayout();
QPushButton *btn1 = new QPushButton();
btn1->setText("btn1");
QPushButton *btn2 = new QPushButton();
btn2->setText("btn2");
layout->addWidget(btn1, 1, 1, 3, 3);
layout->addWidget(btn2, 1, 4);
layout->addWidget(widget, 2, 4);
What is the reason for this strange situation? And how to solve it?
Here is a picture of the result:
And here is wat I want to have:
To build the design you want the first thing is to establish the position of the elements, remember that the position of the rows or columns start at 0, not at 1 as you do. The second part is to set the size policies, some widgets already have some established policy such as the QPushButton that stretches horizontally but not vertically so even if the rowSpan is large it will not change the height of the button, so we must change that behavior and finally the stretch.
#include <QApplication>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSizePolicy>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QGridLayout *layout = new QGridLayout(&w);
QPushButton *btn1 = new QPushButton("Btn1");
btn1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
QPushButton *btn2 = new QPushButton("Btn2");
btn2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow("LineText1 ", line1);
tableLayout->addRow("LineText2 ", line2);
layout->addWidget(btn1, 0, 0, 3, 3);
layout->addWidget(btn2, 0, 3);
layout->addLayout(tableLayout, 1, 3);
// column 0 x3
layout->setColumnStretch(0, 3);
// column 3 x1
layout->setColumnStretch(3, 1);
w.resize(640, 480);
w.show();
return a.exec();
}
Note that the QFormLayout will make the widgets always on top, so it will not necessarily occupy the height of the space offered by the QGridLayout.

QT dynamically generate label, LineEdit, Button

I want to do serial communication in the QT inside, according to the number of serial ports to dynamically generate label, LineEdit, Button, and these three buttons can pull down the scroll bar when the size of the interface, how to do well, I write this below is dead of.
The effect of encapsulation into a method
The interface was washed last
Define QGridLayout insude QScrollArea. And add your new widgets into
that layout in code. QGridLayout::addWidget
Define table where you will show several widgets in table cells. That real complicated way.
void BaseUi::BaseScrollArea()
{
QScrollArea *pArea = new QScrollArea();
QWidget *pWidget = new QWidget();
pWidget->setStyleSheet("QWidget" "{background:white;}");
m_vbox_layout = new QVBoxLayout();
m_vbox_layout->addSpacerItem(new QSpacerItem(100, 30,
QSizePolicy::Expanding, QSizePolicy::Expanding));
pWidget->setLayout(m_vbox_layout);
pArea->setWidget(pWidget);
pArea->setWidgetResizable(true);
m_main_layout = new QVBoxLayout();
m_main_layout->addWidget(pArea);
}
void BaseUi::addAutoRecordUi(QString lab_neme, QString ledit_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}
void BaseUi::addMulRecordUi(QString lab_neme, QString ledit_name, QString
but_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
QPushButton *but = new QPushButton(but_name);
but->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
layout->addWidget(but, 0, 3);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}

QObject::connect: No such slot QWidget::makeyourbox() in occQt.cpp:324

I am new to here.
The ui run well ,but when I click 'okbtn' ...
QObject::connect: No such slot QWidget::makeyourbox() in occQt.cpp:324
And when I click 'cancelbtn', it runs.
Thanks for any responses,
Eason
code:
void occQt::about2() //UI
{
QWidget* pWidget = new QWidget;
QLabel* longlabel = new QLabel(tr("long"));
QLabel* widthlabel = new QLabel(tr("width"));
QLabel* highlabel = new QLabel(tr("high"));
longlineedit = new QLineEdit;
widthlineedit = new QLineEdit;
highlineedit = new QLineEdit;
QPushButton* okbtn = new QPushButton(tr("ok"));
QPushButton* cancelbtn = new QPushButton(tr("cancel"));
QGridLayout* gridlayout = new QGridLayout;
QVBoxLayout* dlglayout = new QVBoxLayout;
QHBoxLayout* btnlayout = new QHBoxLayout;
gridlayout->addWidget(longlabel, 0, 0, 1, 1);
gridlayout->addWidget(widthlabel, 1, 0, 1, 1);
gridlayout->addWidget(highlabel, 2, 0, 1, 1);
gridlayout->addWidget(longlineedit, 0, 1, 1, 3);
gridlayout->addWidget(widthlineedit, 1, 1, 1, 3);
gridlayout->addWidget(highlineedit, 2, 1, 1, 3);
longlineedit->setText("5");
widthlineedit->setText("5");
highlineedit->setText("5");
btnlayout->setSpacing(60);
btnlayout->addWidget(okbtn);
btnlayout->addWidget(cancelbtn);
//pWidget->setLayout(gridlayout);
dlglayout->setMargin(50);
dlglayout->addLayout(gridlayout);
dlglayout->addStretch(40);
dlglayout->addLayout(btnlayout);
pWidget->setLayout(dlglayout);
pWidget->setWindowTitle(tr("Make a Box by custom."));
pWidget->show();
connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(makeyourbox()));
//QObject::connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
connect(cancelbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
}
void occQt::makeyourbox()
{
QString string_a = longlineedit->text();
eason_a = string_a.toInt();
QString string_b = widthlineedit->text();
eason_b = string_b.toInt();
QString string_c = highlineedit->text();
eason_c = string_c.toInt();
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(eason_a, eason_b, eason_c).Shape();
Handle_AIS_Shape anAisBox = new AIS_Shape(aTopoBox);
anAisBox->SetColor(Quantity_NOC_AZURE);
mContext->Display(anAisBox);
}
When I run the pWidget, click cancelbtn, ui close.
Click okbtn,do nothing..
pWidget is a generic QWidget. It does not contain method/slot makeyourbox().
Your code is faulty.
you should add makeyourbox() method to the subclass of QWidget, and mark it as slot
Double check makeyourbox is defined to be a slot within that class.

check QCheckBox in QVboxLayout of QStackedWidget

i have a QstackedWidget and i add QHboxLayout and QVBoxLayout to it, in my vertical layout i add 3 QCheckBox, and i add this layout with two spacer for left and right side to it, then i push this new layout with two spacer for top and bottom in horizontal layout, now i want to check if this QCheckBoxes are checked or not, but i don't know how to check this.
here is my code :
QVBoxLayout *center = new QVBoxLayout();
QVBoxLayout *main = new QVBoxLayout();
QHBoxLayout *middle = new QHBoxLayout();
QSpacerItem *topspacer = new QSpacerItem(14, 24, QSizePolicy::Fixed, QSizePolicy::Fixed);
QSpacerItem *buttom = new QSpacerItem(4, 4, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *left = new QSpacerItem(4, 4, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *right = new QSpacerItem(4, 4, QSizePolicy::Expanding, QSizePolicy::Expanding);
middle->addItem(left);
middle->addLayout(center);
middle->addItem(right);
center->addWidget(checkBoxEvent(QString::fromStdString("Analyze events")));
center->addWidget(checkBoxEvent(QString::fromStdString("Edit Tracks")));
center->addWidget(checkBoxEvent(QString::fromStdString("Analyze players")));
main->addItem(topspacer);
main->addLayout(middle);
main->addItem(buttom);
Qwidget *m_page = new Qwidget();
m_page->setLayout(main);
m_stackedWidget->addWidget(m_page);
i tried this code for accessing to elements but i get segmentation fault:
for(int i = 0; i < m_stackedWidget->widget(2)->layout()->count(); i++)
if(dynamic_cast<QPushButton*>( m_stackedWidget->widget(2)->layout()->itemAt(i) )->isChecked())
this items are added to m_stackedWidget[1]

Qt QWidget disable re sizing more than minimum size needed [duplicate]

This question already has answers here:
how can I fully disable resizing a window including the resize icon when the mouse hovers the border?
(15 answers)
Closed 9 years ago.
I have a QGridLayout inside a QWidget. I keep adding child QGridLayouts to this. I want the QWidget to resize according to the size needed for child layouts but for the user to be unable to resize it.
MainWindow.cpp (MainWindow is inherited from QWidget)
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QGridLayout *mainLayout = new QGridLayout();
{
AAController *ac1 = new AAController("Ins1");
AAController *ac2 = new AAController("Ins2");
AAController *ac3 = new AAController("Ins3");
mainLayout->addLayout(ac1, 0, 0);
mainLayout->addLayout(ac2, 0, 1);
mainLayout->addLayout(ac3, 1, 1);
}
setLayout(mainLayout);
}
AAController.cpp (AAController is inherited from QGridLayout)
AAController::AAController(const QString& instrument)
:_instrument(instrument)
{
_lblInstrument = new QLabel(_instrument);
_lblMismatchThreshold = new QLabel("Mismatch threshold : ");
_lblQuoteVolume = new QLabel("Quote volume : ");
_btnUpdateAlgo = new QPushButton("Update Algo");
_spnMismatchThreshold = new QSpinBox();
_spnQuoteVolume = new QSpinBox();
this->addWidget(_lblInstrument, 0, 1, 1, 2, Qt::AlignCenter);
this->addWidget(_lblMismatchThreshold, 1, 0, 1, 2);
this->addWidget(_lblQuoteVolume, 2, 0, 1, 2);
this->addWidget(_btnUpdateAlgo, 3, 2, 1, 2);
this->addWidget(_spnMismatchThreshold, 1, 3);
this->addWidget(_spnQuoteVolume, 2, 3);
setSizeConstraint(SetFixedSize);
}
I get something like this:
But at the moment I can resize this like:
I want to disable such resizing. How do I do this?
Try this in your main window:
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
This is the result i got: