Get text() from Widget in Layout - qt

I have created widgets from code for the first time instead of through the creator. It reads in a text file and fills a QScrollArea widget with the material name (checkbox to enable it or not) and the thickness lineEdit, as seen in the picture. The number of widgets made corresponds to the number of entries in the text file.
I create the pushbuttons and checkboxes using the following function:
void overlayers::newField(QString material, bool checked, double thickness)
{
//Create new checkbox.
QCheckBox * checkbox = new QCheckBox(material);
checkbox->setChecked(checked);
//Create new field.
QLineEdit * line = new QLineEdit(QString::number(thickness));
//Create horizontal layout.
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(checkbox,Qt::AlignLeft);
layout->addWidget(line,Qt::AlignRight);
//Add horizontal layout to vertical.
scrollLayout->addLayout(layout);
}
Where QVBoxLayout scrollLayout is defined in the class header.
I realised after doing the whole dialog box, I have to get the values from the fields and the checkbox flags to save the data but I have no idea how to iterate through all the checkboxes and lineEdits to get the text() and checkedState() variables!
I feel I have programmed this incorrectly and that there is a better way to do it. Is it possible to do this with how I have done it? If not, how would a better programmer do it re: creating the variable number of widgets and being able to access their data? A QVector sort of thing?
Thanks heaps!
http://i.stack.imgur.com/NKJVW.png

Related

How to add text onto QToolbar with Qt Designer?

I would like to ask, if it is possible to add some text onto QToolBar with Qt Designer?
You could do it in Qt Designer, but it'd be a bad hack an unnecessary. Instead, do it via code. There are two ways:
QToolBar is a widget that graphically represents actions, represented by QAction. You can add an action that is pure text, using the QToolBar::addAction(const QString &). E.g. if the toolbar object is simply called toolbar, you'd write toolbar->addAction("Some text");. This will create a button with given text, and return the corresponding action. Actions are an abstraction: they are devoid of a graphical representation, and they can be a part of multiple widgets (e.g. menus and toolbars). It's the individual widgets that give the actions some "physical shape", e.g. construct a button for each action.
QToolBar, at a lower level, is a collection of widgets, that you can add using QToolBar::addWidget(QWidget *). So, if you want to add some static text, you'd create a label with that text and add it to the toolbar. The action thus created may not seem very useful, since you won't be reacting to the user clicking the text (see p.1 above if you do). But it could be used to show and hide the text if desired, and acts as a handle for the text, allowing you to change the text as well. It is really a QWidgetAction derived class. See below for some code to get you started.
// The text can be empty, in case it was meant to be set sometime later
QAction *addText(QToolBar *toolbar, const QString &text = {})
{
auto *const label = new QLabel;
auto *const action = toolbar->addWidget(label);
// Any changes to the action's text should be propagated to the label.
QObject::connect(action, &QAction::changed, label, [action,label]{
label->setText(action->text());
});
action->setParent(toolbar); // so that the action won't leak
action->setText(text);
action->setVisible(true);
return action;
}
// inside some method
auto *action = addText(toolbar, "Some text");
// sometime later
action->setText("Some other text");
A more flexible way of doing this would be to derive QWidgetAction to create e.g. StaticTextAction, and put the label creation code into StaticTextAction::createWidget(). But such flexibility is most likely unnecessary, since all you're after here is some static text that applies to the toolbar and has no other use (e.g. you wouldn't add it to any other widgets or menus).

Dynamically creating and displaying an array of buttons inside QScrollArea

My aim is to create an array of command link buttons dynamically on clicking a push button in and then display them all inside a vertical layout inside a QscrollArea. I get the data for the buttons from a database. For this I created a slot for the button and wrote the following code inside the slot function.
QCommandLinkButton *slotButtons[10];
for(int i=0; slotQuery.next(); i++)
{
slotButtons[i] = new QCommandLinkButton;
slotButtons[i]->setText(slotQuery.value(0).toString());
slotButtons[i]->setDescription(slotQuery.value(1).toString());
ui->scrollAreaSlots->layout()->addWidget(slotButtons[i]);
ui->scrollAreaSlots->show();
slotButtons[i]->show();
}
This compiles without errors but the buttons are not visible, even after calling show.
Could anyone tell me where I'm going wrong?
Update: If i remove all the "[i]"s and comment the loop; basically creating just a single command link button, it works perfectly. But it doesn't work for the loop. Is everything right with my looping?
The QScrollArea has one child widget which can contain other widgets.
When a QScrollArea widget is created with Qt Creator's UI designer, Qt Creator creates automatically a widget named scrollAreaWidgetContents. Buttons are then added to that widget's layout, which is not created automatically. The layout is created in the following code which also add the buttons:
QCommandLinkButton *slotButtons[10];
QVBoxLayout* layout = new QVBoxLayout(ui->scrollAreaWidgetContents);
for(int i=0; slotQuery.next(); i++)
{
slotButtons[i] = new QCommandLinkButton;
slotButtons[i]->setText(slotQuery.value(0).toString());
slotButtons[i]->setDescription(slotQuery.value(1).toString());
ui->scrollAreaWidgetContents->layout()->addWidget(slotButtons[i]);
}
Try to add the following line (before adding the button to the scroll area)
slotButtons[i]->setVisible(true);
This should make sure that button itself is visible when you add it to the scrool area.

How to set icon of the line edit in a QCombobox widget without inserting the item into the list?

I want to set the icon on the left side of the QCombobox widget. I know I can insert a item first and then set the icon of the inserted item and then select this newly inserted item. However, I would like to do that without inserting a new item into the drop down list for special reasons. Windows ComboBox control allows us to change the icon of the edit box by using an index of -1. I don't know how to achieve that with QCombobox.
Thanks for any comments!
Never tried it myself, but here is an idea.
QComboBox is based on Qt's model/view framework, so the items are contained into a QStandardItemModel which can be accessed with QComboBox::model().
The steps would be:
Instantiate a QStandardItem
Use setIcon() and setText() on the QStandardItem (or use the proper ctor)
When you want to add the item to the Combo list, append it thru the model.
Example:
QStandardItem* item = new QStandardItem(theIcon, theText);
[...]
QStandardItemModel* comboModel = qobject_cast<QStandardItemModel*>(theCombo->model());
comboModel->appendRow(item);

qt - How to add radio button to a QMainWindow

I am creating a small window to get a single input from user. For this I want to use radio buttons. In net I didn't find exact answer. There are some pallets they are adding, so just I want to know how to add QRadioButton to QMainWindow. Can any body help me?
Simply specifies the parent (your QMainWindow) when creating the RadioButton
QMainWindow *w = new QMainWindow();
QRadioButton *radiobutton = new QRadioButton(w);
w->show();
More # http://doc.qt.io/qt-4.8/qradiobutton.html

QLabel embedding in QStatusBar using Qt Designer

Is there any solution to embed a QLabel in QStatusBar using Qt Designer?
I don't believe so. It's fairly simple to add one programmatically, though.
If you're just wanting to show a message, you could use: statusBar()->showMessage(tr("Message Here"));, or alternatively if you really needed a QLabel on the status bar, you could do something along the lines of:
QLabel *label = new QLabel("Message");
statusBar()->addWidget(label);
label would become a child of statusBar(), and appear in the first empty spot from the bottom left (addPermanentWidget(label) would add it to the first empty spot from the bottom right). If you place QLabel label in the classes header (or other var name), you'd be able to access the variable directly later (removing the initial QLabel type from the first line, of course).
It is not possible with Qt Designer. I resolve it by creating label a in Qt Designer and later in constructor of my MainWindows add this line:
Ui::"class name of my MainWindows"::"name of statusBar Object"->addWidget("Object Name of Label");
In my application, the class name of mainwindows is MainWindowsForm, the status bar is named statusBar and the label is named informationLabel. Then I have:
Ui::MainWindowsForm::statusBar->addWidget(informationLabel);
It's not possible even if you would manually edit UI file.

Resources