From what i understood from the internet resources, I could create a Popup menu of QActions on the Qtoolbar by using Qtoolbuttonpopup mode.
So, I created a QMenu and added a few QActions to it by using QMenu.addAction.
After that i have created a QToolButton and set the ToolButtonPopupMode to 2. Followed by setting the QMenu i have created above as the menu for it by using .setMenu(QMenu)
SettingMenu = QtGui.QMenu()
SettingMenu.addAction(Action1)
SettingMenu.addAction(Action2)
SettingButton = QtGui.QToolButton()
SettingButton.setIcon(QtGui.QIcon(QtGui.QPixmap(':/setting.png')))
SettingButton.ToolButtonPopupMode(2)
SettingButton.setMenu(SettingMenu)
from the above code, i am expecting to have a Qtoolbutton on my toolbar and when i click on it, it should pop up a menu with 2 Actions. But when i run the code, all i see is a Qtoolbutton on my toolbar but when i click the Qtoolbutton it doesn't create any pop up menu.
Am i doing this wrong? How do i create a toolbutton that create a pop up menu of actions upon user click?
ToolButtonPopupMode is an enumerating type. All the values in that enum are instances of that type. Because it inherits from int, calling it with an integer returns the same integer. However, you want to set the popupMode property, so use setPopupMode(2).
Related
I am making an application where I want to create a dropdown menu if anyone clicks on the Qaction.
My code
self.navtb = QToolBar("Navigation")
self.navtb.setIconSize(QSize(25, 25))
self.navtb.setMovable(False)
self.addToolBar(self.navtb)
option_btn = QAction(QIcon(os.path.join('images', 'options.png')), "Option", self)
self.navtb.addAction(option_btn)
A QAction can have an associated menu which you can simply set with setMenu(). This menu will pop up (drop down) on click+hold.
Now all you need is to set your button to directly go for the menu on click by altering its popup mode. Note that while the wording pop up is used, the menu will be a proper drop-down menu in a toolbar scenario.
In your example it would roughly translate to:
option_btn.setMenu(...)
self.navtb.widgetForAction(option_btn).setPopupMode(QToolButton.InstantPopup)
For reference, this is how I do it in my code in C++:
// initialize compute menu and let button display menu without holding mouse
actionComputeDisplay->setMenu(new QMenu(widget));
auto btn = qobject_cast<QToolButton*>(toolBar->widgetForAction(actionComputeDisplay));
btn->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);
How to add an 'about' button to the menu bar of your main window - which when clicked directly opens a dialog with some about text - using PyQT?
Or is that impossible?
Having had a look at the documentation/question and answers online having to do with the menu bar, I get the impression that the QMenuBar only supports triggering events via 'QAction's via menu drop-downs. However I dont want a drop-down for the about button but rather would like it to trigger some showAboutDialog method.
If you have any ideas/links please let me know.
You can add a QAction object directly to the menubar of your MainWindow. Use the QMenuBar.addAction() method for this:
class YourMainWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = QMenuBar()
menu.addAction(show_about_dialog_action)
self.setMenuBar(menu)
The code below creates QComboBox and QPushButton both assigned to the same layout. Combobox is set to be editable so the user is able to type a new combobox item's value.
If the user hits Tab keyboard key (instead of Enter) the New Value will not be added to the ComboBox.
Question: How to make sure the ComboBox's items are updated with the New Value even if the user leaves the ComboBox with Tab key?
from PyQt4 import QtGui
def comboActivated(arg=None):
print '\n ...comboActivated: %s'%arg
widget = QtGui.QWidget()
layout = QtGui.QVBoxLayout()
widget.setLayout(layout)
combo = QtGui.QComboBox()
combo.setEditable(True)
combo.addItems(['One','Two','Three'])
combo.activated.connect(comboActivated)
layout.addWidget(combo)
layout.addWidget(QtGui.QPushButton('Push'))
widget.show()
When a user edits the text in the box, the editTextChanged() signal is emitted with the edited text as its argument. In addition, when the widget itself loses focus, as when the user types Tab to move to the button, the widget emits the focusOutEvent() signal. The argument for this signal is a QFocusEvent, which you can query for the reason focus was lost. The reason() method of the event would return Qt.TabFocusReason, for example, if the user hit the Tab button to leave the widget.
You can connect a slot to either (or both) of these signals, so that when the user leaves the widget after editing text, you process it and add it to the box's list of values.
You may also want to look into the QValidator class and its subclasses, which you attach to widgets with editable text, and define the types of valid input for the widget (e.g., integers, text, etc.). This is the best and easiest way to verify a user's input for editable widgets.
I want to make the "Application 1" disappear when the "Single" button is clicked. And show it again i mean both when "Split" button is clicked. Is there any method to make disappear/collapse a widget from QSplitter().
Here's the basic layout :
Thanks in advance.
QWidget has functions show() and hide(), if Application1 is inside QWidget or any other widget inheriting QWidget, you can call hide on the object of that widget when user clicks on Single button, (widget.hide()). When user clicks on Split button you can call show() on the same object to show the widget.
Edit
Another way of achieving this would be:
to set the size of QSplitter. When Single Button is pressed, do the following:
splitter.setSizes([self.width(), 0])
When split button is pressed do the following:
splitter.setSizes([self.width()/2, self.width()/2])
Assuming that the self refer to mainWindow containing the splitter and self.width() gives the width of mainWindow.
I am using pyQT 4.8.3 in order to create a proper GUI for a QGIS plugin
There are three widgets in the form
my_comboBox , my_lineEdit , my_spinBox
Assume that comboBox has three entries
'combo_first_item' , 'combo_second_item' , 'combo_third_item'
What exactly I want is;
if 'combo_second_item' is selected, then my_lineEdit toggles state to disabled
if 'combo_third_item' selected, then my_spinBox toggles state to disabled
So, how can I toggle enabled state of widgets in the form based on selected string (or index value) from the combobox?
What should be the proper signal -> slot assignment?
Unlike QbuttonBox, QcomboBox does not fire SetDisabled slot
Thanks.
Make a dictionary that maps the string to widget:
widgets = {'combo_first_item': my_comboBox,
'combo_second_item': my_lineEdit,
'combo_third_item': my_spinBox}
And a slot:
def disableWidget(currentIndex):
widget = widgets[currentIndex]
widget.setEnabled(False)
# or anything else you want to do on the widget
Then you can connect the currentIndexChanged[QString] signal to this:
comboBox.currentIndexChanged['QString'].connect(disableWidget)
Alternatively you can use currentIndexChanged[int] and a list instead of a dictionary.
PS: If this is inside a class instance, put self accordingly.