Qt hide QLayout (switch between two layouts) - qt

I didn't find solution for my problem with two QLayouts. I need app with QHBoxLayout with possible expandind when I will add new widgets, push buttons, ....
So what I have: One QDialog and two layouts. Now I know that I can't hide the layout.
So I tray just :
layout()->removeItem(firstlayout);
layout()->addLayout(secondLayout);
But when I did this, I saw all items in first layout on possition [0,0].
So next step I try:
for (all items in first layout) if (widget) widget->hide();
But this is working only with QWidget and I have many different items in layouts.
Simply way is use the widget, because there is possible to use hide/show, but I need auto expanding window when I add new items.

Just rebuild the layout, there is no need to keep the two layouts in existence at the same time. Probably clearest is to have two (or more) methods, which first delete current layout, then create new layout, add widgets to it, hide all widgets you want hidden, and set it as current layout. Note that you don't even need to keep a member variable for the layout, since QWidget has that anyway and provides you with setter and getter.
Or, if you have different widgets in different layouts, and actually want to be able "switch pages" so to say, simply use QStackedWidget. Or if you have a fixed part (buttons etc), and then part with "pages", then put the "pages" into QStackedWidget, and keep fixed part out of it.

'addLayout(secondLayout/firstLayout) ' will remove the other layout automatically, you do not have to remove it. If you keep a pointer to the layout(which has addWidget() before), you can simply use the layout and widgets in it later. :)

Related

What is the best way to implement this using Qt

I'm totally new to Qt, so I'd be glad to have a wide answer.
Here I drew up some model:
We have a kind of table that contains:
an integer value with a spinbox.
a cell with three(not specifically) grouped radio buttons
Editbox
A button that interacts with this particular editbox.
Also we have 2 buttons to add and remove items from the table.
I did some google search and found out that it can be done via QTableView.
Is there any way to put such complex structures into a cell? Must it be a separate class inherited from QTableView?
If you're going to have up to a hundred or maybe a few hundreds of elements in the table, then use QTableWidget.
If you're going to have too many elements (about thousands), then go for QTableView, and learn model-view programming.
The reason why I recommend QTableWidget is because you're a beginner. All you have to do there is create a widget, and use setCellWidget() and you're done.
If you have thousands of rows, then you're gonna have to draw the widgets yourself using QStyledItemDelegate, which will paint the widgets inside your QTableView. This is a very painful thing to do, but there's no way around it. The reasons you can find here.
I see at least three options to implement that in Qt:
Use a QtableView or QTableWidget and insert some custom controls in it. See comments made be other persons to your post
Use a QGridLayout and fill it with your controls by line and column
Make your own QWidget to store and manage the line elements (the spinbox, edit field, radio button) using a QHBoxLayout. You can design this in QtCreator, it can have it's own .ui. This could make it easy to handle the interaction between each QWidget of a line (directly handled by your QWidget class). Later, you can put an instance of it for every line you need in a QVBoxLayout.
Personnaly, I would go with the last option, but it may not work smartly if the controls of each line have different content/size (see comments), then first options should be prefered.

How to have common control in QTabWidget

I want to use a common control (QTreeView) in two different tab pages in QTabWidget.
how to do this ?
I added a tabwidget and controls in tab pages in Qt designer.
using qt creator version 2.4.1 in Win 7.
You can't have the same QTreeView in two different QTabWidget pages. When you add any widget to a layout, that layout takes ownership of the widget. Since there can only be one owner, you're stuck with one parent per widget.
But you can fake it. Give your main page a grid layout. Put a QTabBar running along the top, your QTreeView on the left (or wherever), and a QStackedLayout on the right. Connect the tab bar and the stacked layout so changing tabs in the bar changes the visible page in the stack layout.
That should be just what you're looking for - just be prepared to fight with QTabBar to get it to display like you want it to...
Alternatively, just live with having two separate tree views - they'll both be viewing the same model, after all, so the bulk of the data won't be duplicated. Saves you a fight with QTabBar, too.
Hope that helps!

Qt Parent child relationship for independent qmainwindows

Using Pyside, but a general Qt question:
I am building a Qt app with a controlling QMainWindow. From this window the user can open other QMainWindows (or QDialogs) and from some of those she can open more. The user is intended to think of the first QMainWindow as "the app" and the others as lots of different views on more or less the same data.
So I'd like all the windows to be independently stackable so the user can set up the screen to their own requirements. In particular I want the user to be able to bring the first QMainWindow on top if wanted. But I don't really want each window to have its own task bar entry (though I can live with that). Also I would like them to minimise and restore together, and I would like them all to close when the first main window closes.
If I parent them all on the first mainwindow it works nicely except they stay on top of it which is not what I want.
Instead I have it kind of working by making them all independent with parent = None. Then I register them all with the main window and close them all when it closes. But this makes them a bit too independent - they minimise separately and have their own task bar entry.
Am I missing some obvious fix to this? Is there any easy way (a flag?) to stop the children staying on top of the parent?
Or is there some UI guideline that I am breaking by desiring this?
Or is there a cleaner design somehow? I thought of adding a dummy parent that they could all descend from but maybe that's messy. Would that parent need a visual presence? I wouldn't want that.
Suggestions?
You can have as many QMainWindows as you want, or parentless QWidgets. I think the best way to handle your situation is to create your own pseudo parent-child relationship like this:
In your QMainWindow subclass, store a QList of all the QWidgets you want it to manage. Then, again in your QMainWindow subclass, reimplement methods such as QWidget::closeEvent(), QWidget::hideEvent() (for when the window is minimized), and QWidget::showEvent() (for when it is restored) so that it also closes, hides, or shows all of the widgets in its QList. Make sure to also delete them in the QMainWindow subclass's destructor. Now, whenever you create a sub-window, pass the main window a pointer to it not as a normal QWidget child, but just so that it can be added to the main window's QList of QWidgets to manage. E.g.:
MainWindowSubclass::addPseudoChild(QWidget *pseudoChild)
{
myListOfPseudoChildren.append(pseudoChild);
}
Another alternative that hasn't been mentioned yet is populating a QMdiArea with QMdiSubWindows. It doesn't do exactly what you asked for, but it's a pretty clean design nonetheless.
So I thought I would add what I eventually settled upon. This was particularly inspired by the comments of #leemes (Thanks - good stuff) and a little experimentation of my own.
I used the code attached here DetachTabExample
to develop a "Detachable Tab" widget and tab bar. This allows tabs to be dragged outside the main window when they become independent windows. Then if closed they return to the tab bar.
Then I placed all my content in the QMainWindow but in separate tabs. The users can drag the ones they want out on to the other monitor. Seems to be working fine. There are still some extra windows that I have floating but it has cut down the clutter and clarified the structure.

How to update a QLayout and get the new dimensions before returning?

This is driving me nuts. I have a custom menu class that, when set visible, shows a list of items located in a particular folder. When a hardware button is pressed, my application gets the latest list of items, populates the menu with them, and returns.
The menu displaying these items uses a QListWidget filled with custom widgets. Each of the widgets contains one or more QLabels in a horizontal layout, and is created at the time the menu is shown. In order to adjust the text displayed based on the menu width available, I need to get the size of the QLabel AFTER it has been resized according to the layout, but before the menu becomes visible to the user. The problem is, my layout does not get updated until all of the functions constructing my list return.
I have tried QApplication::ProcessEvents() and the layout update functions, but none of them have updated the values of my QLabels before returning. I can set a QTimer when the button is initially pressed, and have it show the menu, update the items, and stop itself, but that seems like a terrible solution.
Any help would really be appreciated! I've spent most of a day on this.
Marlon
I had this exact problem and could not find an answer anywhere on the Internet. Calling Layout.update(), Layout.activate(), or widget.adjustSize() (all suggested in various places) all did not work.
I had a widget with a vertical layout that I wanted to add a QLabel to and then immediately use the size of the QLabel.
The only thing that worked reliably was
layout->addWidget(myLabel);
myLabel->show();
size = myLabel->size();
It would seem that layouts will just not recalculate until you either return from a function and allow the Qt event loop to progress or manually call show() yourself.
How to update a QLayout and get the new dimensions before returning?
Don't. You're not meant to do that. It'll drive you "nuts" because you're doing it backwards. Layout updates are handled asynchronously from the event loop. Instead of getting layout dimensions right away, set yourself up to be part of the system. Some options are:
Implement a custom widget that will interact properly with the layout, growing to fill the available width of the layout. Perhaps all you need is a size policy and a way to elide text?
Make a custom layout that takes the special properties of your use case into account.
You want to call QWidget::adjustSize() on your parent widget. This will force the layout recalculations.
Have you tried using layout()->update(); ?
I've tried many but nothing works for me on Qt 5.15.
Only invented little patch - create timer and get size after 20 msec:
QTimer::singleShot(20, this, [this]
{
const auto height = myLayout->contentsRect().height();
// ...
});

Qt: Adding a special item to QMenu

Some programs like Maya and Wings3D have a menu, which includes a special link on some menu items on the right side (usually a square) that triggers a different feature. For example, Wings3D might have an item called "Cube", which makes a standard cube. However, if you click on the highlightable/selectable box beside the "Cube" item, it will perform a different operation. I attached an image of the Wings3D example.
Is this an easy way to do this using QMenu and QAction? Should I create a special layout for the QMenu or do I need to subclass it to get more fine-grained control?
You could try using QWidgetAction to accomplish this. But it might take a bit of work to ensure that those actions stay consistent in terms of the look & feel with the typical rendering of QActions across different platforms and styles.

Resources