How to set the line where a QToolBar is displayed? - c++

I would like to ask if anyone knows how to display 2 QToolBars in two lines, one on top of the other? I found the class QStyleOptionToolBar, but I don't know how to use it...
It is easy to drag one toolbar with the mouse to be placed below the other, so I think there must be a way how this can be done from the source code as well...
Any hint would be appreciated!
Claus

Try calling QMainWindow::addToolBarBreak(Qt::ToolBarArea) in between adding the two tool bars.

I think that when you add the 2 toolbar's if you place them in the same area, they'll stack automatically:
QMainWindow *mainWin = get_main(); // however you get it
mainWin->addToolBar(Qt::TopToolBarArea, new QToolBar);
mainWin->addToolBar(Qt::TopToolBarArea, new QToolBar);
This should produce 2 toolbars, both at the top.

If you want toolbars to be in two lines you should do it in following manner:
firstToolbar = new QToolBar(this);
secondToolbar = new QToolBar(this);
addToolBar(Qt::TopToolBarArea, firstToolbar);
addToolBarBreak();
addToolBar(Qt::TopToolBarArea, secondToolbar);
addToolBarBreak accepts Qt::ToolBarArea so you can specify for which area you would like to add the break. By default it is Qt::TopToolBarArea.

Related

QT : how to synchronize QtableViews in layouts?

i'm working on a Qt project with an interface and i'm using four QTableViews and for some reason, the last one is not following the others when i stretch the windows. the 4 tableviews have the exact same parameters. they are in layouts, along with other items on the interface. i set the MaximumWidth of my tableview like this :
int hHeaderWidth = ui->tableCSB_Right->verticalHeader()->width();
int rowWidth = ui->tableErr_Right->columnWidth(0);
int tableWidth = (nbOfAnt/2)*rowWidth + hHeaderWidth;
ui->tableErr_Right->setMaximumWidth(tableWidth);
ui->tableErr_left->setMaximumWidth(tableWidth);
ui->tableCSB_Right->setMaximumWidth(tableWidth);
ui->tableCSB_Left->setMaximumWidth(tableWidth);
ui->tableLeft_Coupling->setMaximumWidth(tableWidth);
ui->tableRight_Coupling->setMaximumWidth(tableWidth);
and here's what i get when i launch my program and when i stretch the window:
as you can see the last qtableview goes way farther and the other ones along with its QPushButton and labels. i dont get why does it behave like this. has anyone dealt with this problem ?
thank you in advance

QT QIcon properties for custom widget in designer

I have been working for a little while now on creating a QT custom designer widget for GUI menus. The idea being that you simply drag it into the designer, select the number of frames you'd like, how many buttons per frame, etc. and it generates and sizes everything for you.
The way the widget is structured there are properties to configure each button for the frame you are in. For example, you would use the button0Text field to enter text under Button0 while editing in frame 0, then use it again to edit Button0 which is in frame 1. Both buttons would retain the individual changes for each frame.
The Problem
Normally when I switch frames all of my properties are updated to reflect the status of the frame. The exception being QIcon. The correct icon is retained in the actual graphical representation and builds correctly, however the file path in the property list is always of the last edited for that property. I think this will be extremely confusing to an end user and I have found no way to fix it. So for example, if I set text and icons in frame 0 then switch to frame 1 the text in the property list will update to reflect the state of frame 1 but the icon path names will still show my last edit in frame 0 and not the actual icon in frame 1.
I have tried things as simple as:
setProperty("button0Icon", getButton0Icon());
That code works on properties like text, but not for the icon. I try executing it immediately after changing frames.
I have also tried:
#ifndef Q_WS_QWS
QDesignerFormWindowInterface *form = QDesignerFormWindowInterface::findFormWindow(this);
if(form){
QDesignerFormEditorInterface *editor = form->core();
QExtensionManager *manager = editor->extensionManager();
QDesignerPropertySheetExtension *sheet;
sheet = qt_extension<QDesignerPropertySheetExtension*>(manager, this);
int propertyIndex = sheet->indexOf("button0Icon");
sheet->setChanged(propertyIndex, true);
sheet->setProperty(propertyIndex, getButton0Icon());
}
#endif
And:
int propertyIndex = this->metaObject()->indexOfProperty("button0Icon");
QMetaProperty property = this->metaObject()->property(propertyIndex);
property.write(this, QIcon());
Nothing seems to update the property list in the designer.
I have all properties, including all QIcon properties properly declared in the header file with Q_PROPERTY and assigned getter and setter functions.
To be clear, the icon values are indeed retained through each frame when compiled. So it is functioning, just unclear for most users.
If anyone has any experience with this or any ideas please let me know. Thanks.
I have discovered that QIcon does not store file names/paths. The file names are only used for the creation of the QIcon. I think this is most likely the reason I do not get any feedback in the Property Browser for my QIcon properties.
Instead I have chosen to hide this property in the designer and add three new ones. Three QUrl properties, each of which is used to supply an image file. I use three because I want to construct a QIcon that contains Modes/States for normal, disabled, and pressed operations.
I take each of these QUrls and save them in QStringLists behind the scenes so their values are stored. I then construct my QIcon using the file names provided from the QUrls.
I would much prefer to be using the native QIcon in the designer for this, any thoughts or feedback are appreciated.

QTabWidget I'd like to hid sub-tabs

I have a QTabwidget and 4 QWidget sub-tabs and I'd like to hide 3 sub tabs when I am not using them.
With a 'Enable' button, I want the hidden sub-tabs to appear again.
In order to hide them, I tried removeTab function as below
ui->tabWidget_2->removeTab(3);
ui->tabWidget_2->removeTab(2);
ui->tabWidget_2->removeTab(1);
But then, I don't know how to reinsert the hidden tabs cuz I do not have the pointer to the hidden tabs.
Or is there any other good method to hide them other than removeTab? Please let me know. It'd be really appreciated. Thanks.
You need store "copy" of your tab somewhere and insert this tab again. For example:
QMap<int,QPair<QWidget*,QString> > map;
map.insert(0,QPair<QWidget*,QString>(ui->tabWidget->widget(0),ui->tabWidget->tabText(0)));
//store index, widget and title of tab
ui->tabWidget->removeTab(0);
ui->tabWidget->insertTab(0,map.value(0).first,map.value(0).second);
//restore data
I can't tell you that it is the best approach, but removeTab removes tab but not your widget. So when I used this code(with QTextEdit as widget inside tab for example) and type some words, my tab was successfully restored and I didn't lose my data. If you use QIcon than you need store this icon too.
Alternatively, if you don't mind the tabs being visible so long as they can't be interacted with, you could use the setTabEnabled function.
ui->tabWidget_2->setTabEnabled( 1, enabled );
ui->tabWidget_2->setTabEnabled( 2, enabled );
ui->tabWidget_2->setTabEnabled( 3, enabled );

Use a pointer to switch between two other pointers in Qt Creator Ui

i do have two Labels in my applications. They are both in each tab. Now i want to witch between those labels by using an pointer that switchs between those labels when tab was changed.
my ui_mainwindow.h defines:
ProLabel *imageLabel;
ProLabel *imageLabel_1;
ProLabel *imageLabel_2;
but only imageLabel_1 and imageLabel_2 are shown in the GUI. I added a third Label (imageLabel) to use it as a variable to switch between the Labels (1/2). So I wrote follwing code in a slot which proves tab changed. So if tab is changed, the other imageLabel_1/2 (pointer) should be used in the hole code, when it says: imageLabel.
my slot when tab changed:
if(tab == 0)
{
this->ui->imageLabel = this->ui->imageLabel_1;
}
else{
this->ui->imageLabel = this->ui->imageLabel_2;
}
I also set the imageLabel_1 whe MainWindow is created. The Window disapear like normal. But if I try to load an image (than it should display the image in the imageLabel) it crashes.
Don't know if it was understandable. Does anybody has an idea if i can handle the pointers like that.. or how to do it different?
Thank you!
Two simpler ways: 1) Change the label contents, either the image or the text it contains, or 2) set the label to hidden with ui->label2->isVisible(false).

QListWidgetItem with Radio Button

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.