I'm new to PySide and Qt at all, and now need to create an application which has a tree view with styled items. Each item needs two lines of text (different styles), and a button. Many items are supposed to be in the view, so I chose QTreeView over QTreeWidget. Now I managed to add simple text items (non-styled) to the QTreeView and have almost no idea about how to place several widgets on one item. Could you please give me an example of how to create such design?
I've found some samples on the Internet, that are similar to what I want, but they all are in C++, and it's not obvious how to convert delegates and other things to Python. I'm now really confused about it all...
I'd recomend you use simple QTreeWidget and insert complex widgets with setItemWidget. While Qt's widhets are alien, they are not so heavy to draw, but:
You shouldn't create delegates.
You shouldn't handle events (If you are going to place button in view and draw it using delegates, you had to handle all its events, such as mouseOver, focus changing, etc. It is a lot of work.
Related
I am fairly new to PyQt5 or rather to Qt in general, so sorry if this question is really dumb or basic.
I am assigned working on an application for some rather complex scientific data analysis that uses PyQt5. Now my current task is to create an interface to enter all kinds of parameters in a treelike structure. I use a class TreeView(QWidget) for this purpose, loosely following this guide: http://pharma-sas.com/common-manipulation-of-qtreeview-using-pyqt5/
Now my boss wants that for some of the items in the tree it should be possible to click them just like a QPushButton to open a submenu, in which one can enter some more parameters (could be a QTableView I guess). As far as I know it is not possible to append QPushButtons just like QStandardItems right?
So what would be the best way to do this? Is it even possible?
There is no real problem, I have just a cosmetic problem. Well, first I create a form with Qt Designer. On this form I place a QTreeWidget. We are still in Qt Designer. In QTreeWidget, I make a few entries (parents), and then I also take a few child entries. When I am done, I save the ui-file and then I load this file - using loadUi() dynamically. In this case I use PyQt4. Up to this point no problem.
When I load the form with placed QTreeWidget I want the program to expand all child entries. By default, when I run the program I only see the parent entries. When I want to see the child entries I have to click on the parent entries, but I don't want this.
Is there a way to solve this?
There is an expandAll() method, which QTreeWidget inherits from the QTreeView class. So, after calling loadUi(), just add something like this:
self.treewWidget.expandAll()
Is there some way to make sure that updating an existing QTreeWidgetItem or inserting a new one ensures that the item is unique among its siblings based on the value of a single column?
I have tried to do something like this with a QTreeView and my own custom model and have gotten it to work, but some advanced features like moving items with drag and drop are eluding me. I figure, the QTreeWidget and QTreeWidgetItem classes ought to get more of this type of stuff for free and ought to cause me fewer headaches throughout. However, I still need to be able to guarantee uniqueness if I go the QTreeWidget route. Can this be done? If so, how?
NOTE:
I am using PySide, but I marked this question as PyQt as well since most solutions cross over between the two without much fuss.
I am trying to build a GUI which will:
Load a file with parameters which describe certain type of problem.
Based on the parameters of the file, show only certain tab in QTabwidget (of many predefined in Qt Designer .ui)
I plan to make a QTabwidget with, say 10 tabs, but only one should be visible based on the parameters loaded. Enabling certain tab is not an option since it takes to many space and the disabled tabs are grey. I do not want to see disabled tabs.
Removing tab could be an option but the index is not related to a specific tab so I have to take care of the shift in the indices. And furthermore if user loads another file with different parameters, a good tab should be added and the current one removed.
My questions are:
How to do this effectively?
Is it better to use any other type of widget?
In Qt designer, is it possible to define many widgets one over another and then just push the good one in front. If yes, how? And how to edit and change any of them?
If using RemoveTab, how to use pointers on tabs, rather than indices?
I use PyQt4
Use a QStackedWidget, which is exactly the same as a tab-widget, but without the tab-bar (which you don't need).
This widget is available in Qt Designer. The context menu has several commands for adding/removing pages and so forth. Note that the arrow buttons in the top-right corner are just there for convenience: they won't appear in your application.
Pages can be added/removed at runtime with addWidget/removeWidget:
index = self.stack.addWidget(self.page1)
self.stack.removeWidget(self.page1)
You can access the pages using either indexes or widget references.
I see that this thread is kinda old. But I hope this will still help.
You can use the remove() method to "hide" the tab. There's no way to really hide them in pyqt4. when you remove it, it's gone from the ui. But in the back end, the tab object with all your settings still exist. I'm sure you can find a way to improvise it back. Give it a try!
I'm working on a PyQt program where I want to be able to have some objects, say shapes, in a 'toolbar' of sorts. I would like the user to be able to click and drag an instance of one of these shapes from the toolbar to a main canvas (a QGraphicsView).
For the toolbar I was thinking of using a QListView which I could populate using a QAbstractListModel. However, I'm not sure how I can make the QListView hold only the icons of the shapes that I want... and second of all, how I can make the object icons draggable onto my main canvas. Any ideas?
If this question contained some specific code examples with an existing direction, I could comment more specifically, but here is just some general points:
Your QListView data does not need to specifically relate to the drag and drop issue, so you can populate it however you want: QListWidget, QListView + model, ../
The important aspect is setting up drag methods on your QListX widget to set up an appropriate QMimeData in the QDrag. This is the important part that transfers the data to the drop site. You can have it send an image, or some reference to some data, for which the receiver can make use of.
Set up a drop event on the graphics view to receive and check the mime type. Lets say you are just sending an image. You can receive it and add a pixmap item to your scene. Or maybe you are sending some internal reference like an id to a dictionary. It can be looked up and the data can be added to the scene in the form you want.
Refer to the general Qt Drag and Drop docs. They are pretty much what you want.