I just started with PyQt and I'm trying to sort elements in a QGridLayout. In the code below, I'm moving an existing button from the cell (4,1) to (0,0). My confusion arises because either using removeWidget or not, both pieces of code behave exactly the same:
myButton = self.myGridLayout.itemAtPosition(4, 1).widget()
self.myGridLayout.removeWidget(myButton)
self.myGridLayout.addWidget(myButton, 0, 0)
is the same as:
myButton = self.myGridLayout.itemAtPosition(4, 1).widget()
self.myGridLayout.addWidget(myButton, 0, 0)
Is PyQt handling everything behind the scenes or is there something that I have not noticed happening?
Thanks
A widget can only belong to one layout. Before a widget is added to a layout, Qt will check to see if it has ever been in a layout, and if so, it will remove it from whatever layout it currently belongs to. The widget will also be automatically reparented to the layout's current parent-widget.
A widget also cannot be added to the same layout twice - but that is really no different from the case above, and so it is treated in exactly the same way.
Related
For better understanding, I brought you a edited picture.
In general, is it possible to place a widget in front of another widget?
For example, on the picture above, you can see a QProgressBar placed in front of QTreeView.
Imagine a situation in which all records are loaded into QTreeView. During this process, the user should be informed how far the loading process is. I know that you can also place the QProgressBar above or below the QTreeView. But I wondered if it's also possible to place the QProgressBar in front of QTreeView?
Yeah it is possible to place a widget in front of another widget.
First of all drag a widget(Suppose a QTreeView) in a main window.
Then right click on the QTreeView and then click on Layout Alignment.
Then choose the alignment left or right and after that drag the another widget(Suppose QProgressBar) in front of your first widget.
I have an QMdiSubWindow which contains a couple of widgets. One of these widgets is a QWidget with a QGridLayout() which can contain an arbitrary number of sub-widgets which is determined at runtime (initially none). I can't seem to workout how to get the MDISubWindow to automatically resize when the number of sub-widgets in the grid layout changes. Should I be re-implementing the sizeHint() somewhere? ie in the main widget or the sub-widgets?
The QMdiSubWindow resizes fine when I drag the resize handle with the mouse and snaps to show the correct size.
I've tried calling .resize() and .updateGeometry() on both the widget and the QMdiSubWindow but It doesn't obviously work. Any clues would be much appreciated.
#ekhumoro suggested I try adjustSize(), which didn't work initially. I did some more searching and ended up with the following solution which worked for me
QTimer.singleShot(1, self.parent.windows[self.uuid].adjustSize)
where
self.parent.windows[self.uuid]
is the QMdiSubWindow object. I'm guessing that just calling self.adjustSize() doesn't work because the sizeHint is not updated until the later in the event queue.
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.
I'm trying to add a tooltip to an image. Following a PyGTK tutorial it seems that this should work:
image = gtk.Image()
image.set_from_file(image_path)
box.pack_start(image, expand=False)
tooltip = gtk.Tooltips()
tooltip.set_tip(image, "Hello!")
Except it doesn't. Nothing happens when I mouse over the image. However, I know that works with buttons (I ran the sample code from the tutorial).
With GTK 2.12 and above, I could probably just use image.set_tooltip_text("Hello!") but I'm stuck at 2.10.4 and have to use gtk.Tooltips.
Edit
According to the documentation for gtk.Tooltips:
Tooltips can only be set on widgets which have their own X window. To check if a widget has its own window use widget.flags()>k.NO_WINDOW. To add a tooltip to a widget that doesn't have its own window, place the widget inside a gtk.EventBox and add a tooltip to the eventbox instead.
So that solves my problem but leaves me a bit confused. I checked the flags for a button and it has the same gtk.NO_WINDOW flag that images have. So why don't buttons need an EventBox but images do?
To satisfy its interface, GktButton creates an event box (well, something like an event box) for itself, internally. I.e. it captures events in a non-visible gdk window. GtkImage doesn't have a similar interface to satisfy so it doesn't need to capture events.
Perhaps it's an accident of the button's internal implementation that using the tooltip interface works without embedding a button in an EventBox or perhaps the tooltip interface actually depends upon a gdk window whether it's visible or not and the Widget interface lacks that sort of flag.
I am trying to write a text viewer widget with PyGTK that displays line numbers alongside the main viewing window. Of course I want the line numbers and main window to scroll in sync with each other. I can't figure out how to get this to work, though. Right now I am doing this. TextViewer is a subclass of HBox that creates the two TextViews and packs them into itself under the attribute names linenums and mainview.
self.textviewer = TextViewer.TextViewer(self.toplevel)
sw = gtk.ScrolledWindow()
sw.set_vadjustment(self.textviewer.mainview.get_vadjustment())
sw.set_hadjustment(self.textviewer.mainview.get_hadjustment())
sw.add_with_viewport(self.textviewer)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
If I take out the two set_Xadjustment lines, then the embedded TextViews' scroll_to_mark function doesn't work, which isn't acceptable for my application. With them in, the main text window scrolls twice as quickly as the line number window, and vice versa if I set the ScrolledWindow's adjustments to those of self.textviewer.linenums. I strongly suspect that this is a bug. I also tried setting up the viewport myself and setting its adjustments to those of one of the TextViews, but again the scroll_to_mark functions stop working. How can I synchronize both TextViews to scroll as one, so that any scrolling changes to one of them equally affect the other?
EDIT: Here is the code in my main application where I set up the widget.
self.textviewer = TextViewer.TextViewer(self.toplevel)
sw = gtk.ScrolledWindow()
#These are the lines that toggle between the two problems when (un)commented
sw.set_vadjustment(self.textviewer.mainview.get_vadjustment())
sw.set_hadjustment(self.textviewer.mainview.get_hadjustment())
sw.add_with_viewport(self.textviewer)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
I'm having difficulty understanding exactly how you've got the two gtk.TextViews packed in the HBox. Are they both packed in separate gtk.ScrolledWindow that are then packed inside of the HBox which is then packed into another gtk.ScrolledWindow(The one mentioned in your post)? From what it sounds like to me, both of these gtk.TextViews are packed in their own gtk.ScrolledWindow within your TextViewer wrapper widget.
If this is the case, a simple solution to your issue, granted that the two gtk.TextViews are the same height(so the line numbers line up with the main view), I suggest simply packing them inside your Hbox without ScrolledWindows. Then you can use your code above, adding them that ScrolledWindow and the viewport will move the two collectively as if they are one widget.
If this isn't your issue, could you please supply some more information about your TextViewer wrapper, and maybe some more sample code?
Also: You may be interested in gtksourceview. With the gtksourceview2 package, you get an instance of the View widget:
import gtksourceview2
view = gtksourceview2.View()
You might want to check how it is implemented in Meld. In particular, the filediff code (search search for sync there).