act on all children at once on pyqt widget - python-2.7

is it possible to somehow obtain possibility to control all the children of a certain Qt.Widget?
For example in a Qgroupbox I have many QtextEdits that I want to clear (clear contents, not delete) at once?

I don't think this is possible. However, you can easily find the child widgets of your groupbox and iterate over them. I think that is your best option.
Something like this:
[textEditor.clear() for textEditor in myGroupBox.findChildren(QtGui.QTextEdit)]
You can add a name parameter to findChildren if you want to filter further by the objectName property of your QTextEdits.

Related

Proper way to find out which Tab of QTabWidget is currently selected

I have a QTabWidget with several tabs, every tab containing a QTableWidget with numbers. Outside of this QTabWidget, I have one button. When the button is pressed, the currently selected widget should be processed. The QTableWidgets are identical in their structure, they just display numbers, and if I have a pointer to one of these widgets, I cannot deduce which tab it came from.
A simple method to identify which widget is currently selected is to call currentIndex(). But this will break when I reorder the tabs in the designer and is probably not working with movable tabs.
Another way is to use currentIndex() and tabText(int index). Like this I have a stable way to find out which tab is currently selected. The disadvantage of this is that I am then dependent on having unique tab texts, and I in general don't like to rely on a UI property to implement functionality.
My solution for now is to give every Widget a different accessible name that I can then read out like this:
QWidget* widget = tabWidget->currentWidget();
QString* name = widget->accessibleName();
This works, but I wonder if there is a better solution, something like the Qt::UserRole that can be assigned to any cell in a QTableWidget.

Implement as custom widget or try to reuse QListWidget?

I'm creating a control that can be used for manual inspection of Tesseract-OCR's output.
Currently I've implemented it as a custom widget, where I do all the drawing (I use a QScrollArea to handle the scrolling at least), and it looks like this:
As you can see, the images are "flowing", like word-wrapped lines in a text editor, and I use the space below the header text, so I don't have 2 clearly separated columns.
Now, it was suggested to me that using a custom widget is 'reinventing the wheel', but the problem is, I don't see a clear way to customize one of the existing container widgets enough to look and behave like this, that would involve significantly less effort or code. Neither is it clear at all to me what is the best way to do it. At most, I can think of using a QListWidget with owner-drawn items and variable item heights.
Am I wrong to use a custom widget here?

Creating a List of QPushButtons

I'd like to create a list of QPushButtons that are added and removed at runtime. I figure that an item view widget would accomplish this (QListWidget). The reason for wanting to use an item view instead of a layout is that I would like to scroll through a list of buttons instead of just trying make them all fit. However, I don't see too many examples of QListWidgets being used to store a QPushButtons.
I'd like some tips, pointers, or examples.
I think you should not use QListWidget in this case. Create a widget with a layout and all your buttons. Then put in QScrollArea.
You can add widgets to QListWidget using its setIndexWidget function.

create a scrollbar in a submenu qt?

I have a map application and a submenu which has dynamically added objects (i.e. points on a map) added to the submenu, depending on the layer that is loaded. I have the ability to hide each individual objects (i.e. a point) by clicking on the corresponding submenu item. Is there any way to organize the submenu? When there are a lot of points (i.e. 100) the entire submenu takes up the screen. Can I add a scrollbar to a submenu? I looked in the documentation, but couldn't find anything that support this feature.
From this bug report I was able to find out that you could do the following:
submenu->setStyleSheet("QMenu { menu-scrollable: 1; }");
Works like a charm.
There is no such possibility as far as I know.
Maybe you shouldn't use a sub menu for this but prefer a menu entry that show a Point manager GUI of your own which would have a QListWidget displaying all your points items.
I'm aware that this solution will break a (big?) part of your code but I don't see anything else.
I think you may be able to get the effect you want by creating and using your own QStyle subclass (via QApplication::setStyle()), and overriding the styleHint virtual method to return 1 when the StyleHint parameter passed in is SH_Menu_Scrollable. At least, that works for me when I create large QMenu objects and show them as popup menus.... It may also work for QMenus attached to the menu bar, but I havent tried that.
Whilst it is possible by subclassing the QMenu class to create a custom widget and going from there you're better off looking at a better way to display that information. You'll save yourself time and it'll be much easier for your users to not have to scroll through a big list of items in a small area.

Show a download list with qt4

What is the best way to show a list of downloads using QT4?
1 QListWidget
2 QTreeWidget
3 QTableWidget
Thanks
It depends on how much functionality you want. If you just want to simply list the filenames, use a QListWidget. If you want to list other things like progress, filesize, etc. then you may want to use a QTableWidget. If you want to be able to group downloads under different categories, you can use the QTreeWidget, which will also allow for multiple table cells per row like the QTableWidget.
If you want something fancier like the firefox downloader, you may have to create an item delegate to handle the drawing yourself.
Use the model-view-controller widgets.
QListView
QTableView
QTreeView