Creating draggable icons in a listview in PyQt - python

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.

Related

Two different QTreeViews with one shared QStandardItemModel

I have a simple task but am struggling with the proper way to implement it:
I want one shared QStandardItemModel containing a tree structure to be displayed in two different QTreeViews.
The first QTreeView shows the model as-is and allows the user to add, remove and rearrange the items. Clicking an item shows an item-associated QWidget within a QStackedWidget below the QTreeView. Some item types can also be renamed.
The second QTreeView shows the same tree model, however all items are greyed out by default. A Processor will then move along the tree structure and process each item. Whenever an item is processed, it should not longer be greyed out. Those items not greyed out should be clickable to display the item-corresponding processed image in a viewer. Whenever a change is made in the first QTreeView, the Processor starts processing from the changed position onwards again.
How do I implement the different look and functionality of the two QTreeViews? Do I assign custom QItemDelegates to each QTreeView? Should I use a QIdentityProxyModel for the second QTreeView?
It is only necessary to use a QStandardItemModel for both models since they store the same information so if you want them to look different then you must use delegates.
If you want some action when the view is clicked then that does not depend on the model but you have to use the view clicked signal that is independent of the other view.
For the second view if a delegate is required to change the color, the logic is to use a role to indicate if that item has been processed or not, and then change the color based on that role. When you say "a change in the first QTreeView" you should actually say "a change in the model" so it is only necessary to use the dataChanged signal, process the item by changing the role status before and after processing.

Buttons in Glade, why are they square?

I'm new at GTK. I'm using Glade to make an interface and just want to insert objects (like buttons and others) into a container.
I've choosen GtkFixed so that all elements can be freely located and sized.
The problem is that when i drag a button for example, the button apparently gets it size from the parent (in this case the container) and takes a square shape.
Here's an image
I want the button to take the standard size, just like a rectangule or something. What i'm i doing wrong? I've checked some options in the container but can't figure out the way.
Thanks.
You have to remove the tick from Height request and Width request (I don't know the equivalent names in your language).
Besides, you should be aware that GtkFixed is difficult to use when the window can be resized. GtkBox is often easier.

Python: Creating own Widget with scrollbars

I'm working on a X-plotter like widget that plots incomming data live.
I already searched for an solution to realize a scrolling along the x-axis if the widget has to much values and so they don't fit.
I had the folling approaches to realize it:
derive from a widget that supports scrolling and child widgets and add my own widget while making it bigger and bigger during the live updates: -> Which parent do I need to use and how do I avoid to draw all the stuff that is currently not visible?
modify my widget in a way that it supports the scrollbars itself -> but how?
draw/handle my own scrollbars -> worstcase :(
I really searched the web for suggestions or examples, but there is nothing about how to "construct" custom controls in a good way (beyond drawing something) esp. in the case of interaction...
Sorry but I'm a newbie at GTK in general :/
Most widgets in Gtk do not have scrollbars.
If you want to scroll the entire widget, you have to implement the GtkScrollable interface. Then, you add the widget to a GtkScrolledWindow. The scrolled window has the scrollbars, those GtkScrollbars are linked with GtkAdjustments which are passed to your custom widget through the GtkScrollable interface set_vadjustment and set_hadjustment.
If you just want to add a scrollbar and control its behaviour yourself, then you need to somehow add a GtkScrollbar in your widget, which means you will need to make it a container too.
The GtkScrollable approach is the following, first you implement vadjustment and hadjustment setters and getters, then when the GtkAdjustments are set, you set its lower and upper limits and the page size(how much of the widget is visible at once). After that, you connect their value-changed signal so you can refresh your widget when the scrollbars are dragged. A GtkScrollable doesn't get to check the scrollbars, only the adjustments that will be bound to the scrollbars. When drawing the widget you get the adjustments' value property in order to determine how much the scrollbars have shifted in the horizontal and vertical axes.

Customize QTreeView items

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.

How can I achieve layout similar to Google Image search in QT (PyQT)?

I'm new to QT. I'm using PyQT for GUI development in my project. I want to achieve this layout in my application. This application searches images from an image database. Google image search layout is ideal for my purpose.
I'm following the book "Rapid GUI Programming with Python and Qt" and I'm familiar with layouts. I guess I need to use a grid layout with each result image in each box of grid. & use vertical layout for (image,Qlabel,Qlabel) inside each grid box.
These are some problems I'm facing.
Importantly, I'm unable to display image. What control/widget do I need? I cannot find anything similar to PictureBox of .NET
How do I seperate these image result by fixed gap like in the image? I'm using Horizontal & vertical spacers but that isn't working?
How to set QLabel a clickable (like hyperlink). I don't want to a open a URL. Just the text should be clickable. So, that when user clicks on the link. I can show more information (like next set of results when he clicks on next page number or a new window with image in fullsize when user clicks on 'view') Do we have some new kind of control for this?
This is another important issue. I'll display the page numbers of results (like shown in figure) & assuming they are clickable. How do I load a new page of results? I mean what is the equivalent of page in QT?
As you can guess. This definitely wont be the first page of GUI. The first page will be exactly like http://google.com (a big logo & text box with button below it). when user clicks the search button. This page will be displayed. Again the same question comes up. How change the pages?
Please give a list of controls I'm going to need for this. Tell me if I'm unaware of something.
1/2.
For displaying the images and labels use a QListWidget with view mode set to QListView::IconMode. However, if you need to customize the display beyond what the QListWidget/QListWidgetItem api can provide you will need to create your own QAbstractListModel and use a standard QListView with it.
Make sure and read Qt's primer on model/view.
As for spacing the images, checkout the spacing property on the list view.
Here is an example from KDE's Dolphin file manager:
3. Use a regular QLabel, but set the contents to be an href.
Example:
edit: Oops I see from your tags you are using PyQt, the following is C++, but should be similar to what you would do in python.
QLabel *linkLabel = new QLabel;
linkLabel->setTextFormat( Qt::RichText )
linkLabel->setText( " Click me! " );
connect( linkLabel, SIGNAL( linkActivated ( const QString & link ) ), .... )
4.
Well, since you are using a Model/View, why bother having page numbers at all? The user will just be able to scroll the view and more pictures will be shown. This is by far the easiest solution as you don't have to do anything once you've got your M/V setup!
However, if you really want to show page numbers it will require more work in your model. For example, have a track the "current page" in the model and only allow access to images on the "current page". Then in your slot connected to the linkActivated() signal tell the model to change pages. I won't go into much more detail as this seriously violates the whole idea behind model/view. The "right way" of doing this would be to subclass QListView and add pagination support, but like I said why not use scroll bars? There isn't any performance hits to doing so.
5. Use a QStackedWidget, addWidget() all your "pages" to it, then call setCurrentIndex/Widget() as needed to switch the pages.
Thoughts:
It seems you are very committed to cloning the look, feel, and behavior of Google Image search, which is fine, but Google Image Search is a web application that uses interaction paradigms that are very different than a normal desktop application (links, pages, etc). You are presumably developing a desktop application, and by trying to emulate the behavior of a web app you will find it difficult as the API just isn't designed to support those sorts of interactions. By all means, it is doable, but you'll have your work cut out for you.
If you are extremely intent on sticking to the web based interaction style, why not code your app in javascript and HTML and toss it in a QWebView?
Try using QListWidget with viewMode set to IconMode. It should do all for you. BUT if you need to customize your data display use QListView with your own/standard model and own delegate for painting

Categories