I'm working in a QT project. There are two Widgets (QFormLayout) inside a QVBoxLayout (just a simple box). Each QFormLayout contains rows with two columns: a label and a Qcombobox, and each QFormLayout can have one or more rows. The first QFormLayout can be added or not, so this is the reason to have two QFormLayout instead a single one. Also, it is important to mention that each widget are created/defined in different classes. So, unfortunately I can't change the current design.
First widget
auto settings = new QFormLayout(this);
auto labelName= new QLabel("Some text");
auto combo1 = new QComboBox(this);
settings->addRow(labelName, combo1);
Second widget. In this example the second widget have two rows perfectly aligned.
auto options = new QFormLayout(this);
auto labelName= new QLabel("Some text");
auto combo2 = new QComboBox(this);
options ->addRow(labelName, combo2);
As you can imagine I'm getting the following result:
there is a way to align and keep the same size of the combobox that are in different widgets?. Maybe align the combobox to the right side could help. I'm trying the following but the result is not good (the combobox width is to small).
settings->setAlignment(combo1, Qt::AlignRight);
Related
I've seen and tried various QT scrollArea solutions over the past 2 days but none of them work for me. Here's my scroll area setup code as it stands in the MainWindow constructor. This builds and runs without error but doesn't do anything. The scrollArea and ui->Contents have already been set up in the form using QTcreator and the needed widgets have been moved into the scrollArea.
ui->scrollArea->installEventFilter(this);
ui->scrollArea->setMouseTracking(true);
ui->scrollArea->setWidget(ui->Contents);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);
ui->scrollArea->setLayout(layout);
The last line seems interchangeable with:
layout->addWidget(ui->scrollArea)
but neither one changes the result, which is a fully-functioning application but without the scroll area I need.
I had similar problem which i solved by creating scrollArea and it's contents via code rather than form and only then using setWidget() method. I described the problem in this thread.
In your case code should look something like this:
QScrollArea *scrollArea;
scrollArea = new QScrollArea(this);
scrollArea->installEventFilter(this);
scrollArea->setMouseTracking(true);
scrollArea->setWidget(Contents);//whatever Contents is, i recommend creating it via code
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);
scrollArea->setLayout(layout);
I am working on a Project using Qt 5.7 with C++. I am stuck in a weird problem.
I have a QTableWidget which contains a QComboBox on its one and only cell. Simplified code is as follows.
QTableWidget *tab = new QTableWidget();
tab->insertColumn(0);
tab->insertRow(0);
QComboBox *cb = new QComboBox(tab);
cb->addItem("A");
cb->addItem("B");
tab->setCellWidget(0, 0, cb);
Now on currentIndexChanged(int) signal of the QComboBox, I am connecting to a SLOT where I am trying to retrieve the pointer of the QTableWidget as follows,
QComboBox* cb = qobject_cast<QComboBox*>(sender());
QWidget* par = cb->parentWidget();
But, I am not getting the same pointer as the actual QTableWidget.
I have also tried as follows, but still failed.
QComboBox* cb = qobject_cast<QComboBox*>(sender());
QObject *par = cb->parent();
QTableWidget *tab = qobject_cast<QTableWidget *>(par);
Can anyone suggest some other way to do it or point out the error in those code segment ?
Possible Solution Found
Parent Widget can be retrieved using cb->parent()->parent(). Although, this process is not documented, so, not reliable.
The reason is that the QTableWidget is not a direct parent of QComboBox, but a grand parent. But even this is an implementation detail and cannot be relied upon, because it is not documented.
A way out could be storing the QTableWidget pointer somewhere else.
I am using qt to develop an embedded gui application. I am using 2 QStatusBars to make a menu-like buttons one can see on an osciloscope for example:
My problem is I dont know a proper way of separating the buttons from eachother with a certain width. In the picture you can see I have added couple separators to achieve that, but it doesnt look that way when run on the target.
Is there a better way to separate buttons on QStatusBar with certain width?
I'd prefer you use a blank widget to do the seperation as suggested by Martin, like so;
//the 2 widgets in the status bar
button1 = new QPushButton("Button1");
button2 = new QPushButton("Button2");
//the blank widget. You can set your width with 'setFixedWidth(int)'
widget = new QWidget;
widget->setFixedWidth(50);
widget->setHidden(1);
widget->setVisible(1);
//placing them in the status bar
statusBar = new QStatusBar;
statusBar->addWidget(button1);
statusBar->addWidget(widget);
statusBar->addWidget(button2);
I'm working on my first QT application and I have a problem with QListWidgetItems.
I will be having different kind of list.
for checkboxed list using:
listElement[i]->setFlags(Qt::ItemIsEnabled);
listElement[i]->setCheckState(Qt::Unchecked);
works exactly as wanted.
But now I want a Radio Button list. so my question is in two parts
can use the same logic that I used for checkBox to create Radio Buttons?
I have used:
listElement[i]->setFlags(Qt::ItemIsEnabled);
QRadioButton *radio1 = new QRadioButton(0);
dlList->setItemWidget(listElement[i],radio1);
this will display Items in the list with a radio Button, the problem is that the text is Over the Radio Button:
going to try to demonstrate without image
This is a test
o
for elements 1
instead for checkbox I have
This is a test
[]
for element 1
how can I get the radioButton to align correctly with text?
New Questions:
Thanks alot for the answers my text is next to my RadioButton now.
Only thing there is no WordWrap, My text is Longer than maximum Size of the RadioButton. How can I get it to wordwrap:
rButton = new QRadioButton();
rButton->setFixedSize(LIST_TEXT_WIDTH_WO_ICON, LIST_TEXT_HEIGHT);
rButton->setStyleSheet("border:none");
rButton->setFont(segoe18Font);
rButton->setText("This is just a test for elementsss of type euh!!!");
rButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
dropListWidget->setItemWidget(listElement, rButton);
As you may have read, there are two approaches to achieve what you want.
The most flexible one: use a QListView, implement a new delegate and a model if necessary.
Keep using the classic item-based interface (QListWidget) and change the item's widgets either by sub-classing QListWidgetItem or calling QListWidgetItem::setItemWidget.
Since the question points towards the second one, I'll try to provide the simplest item-based solution.
The following piece of code generates the list widget in the picture.
QListWidgetItem *it;
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item 1")));
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item 2")));
// .
// .
// .
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item N")));
where ui->listWidget is a pointer to the QListWidget that holds the items.
I hope this helps. As far as I understand, that's what you need.
I have an issue, when I'm trying to add a QLabel and a QPushbutton object into a QVBoxLayout. The problem is, that it adds too much space between them like in the picture
Here is a code example of creating the layouts and the labels, and adding them. I'm adding the buttons later on, but that is just with another ->addWidget(button).
jobbcimke= new QLabel(trUtf8("Jobb oldal"));
jobbkozepcimke= new QLabel(trUtf8("Jobb part"));
balcimke= new QLabel(trUtf8("Bal oldal"));
balkozepcimke=new QLabel(trUtf8("Bal part"));
jobbfelulet=new QVBoxLayout();
jobbkozepfelulet=new QVBoxLayout();
balkozepfelulet=new QVBoxLayout();
balfelulet=new QVBoxLayout();
osszefogo=new QHBoxLayout();
jobbfelulet->setAlignment(Qt::AlignRight);
jobbkozepfelulet->setAlignment(Qt::AlignRight);
balfelulet->setAlignment(Qt::AlignLeft);
balkozepfelulet->setAlignment(Qt::AlignLeft);
balfelulet->addWidget(balcimke);
balkozepfelulet->addWidget(balkozepcimke);
jobbfelulet->addWidget(jobbcimke);
jobbkozepfelulet->addWidget(jobbkozepcimke);
osszefogo->addLayout(balfelulet);
osszefogo->addLayout(balkozepfelulet);
osszefogo->addLayout(jobbkozepfelulet);
osszefogo->addLayout(jobbfelulet);
setLayout(osszefogo);
How could I remove the space between them, or is there a better method to do this? I've created the labels for the layout size allocation.
If you dig into the documentation there's a setSpacing(int x) method for QLayouts that allows you to edit the spacing in between the elements, you may also need to add some QSpacerItems to get the positioning exactly where you want it, or apply constraints with the setGeometry(QRect rect) method.