PyQt: display QPushButton that looks like the down-arrow button in QComboBox - python

In PyQt4, I want to present a QPushButton that looks like the down-arrow button in QComboBox. Feasible, and if so, how?
I don't need help getting my new widget-combination acting like a QComboBox (see below). I only want the QPushButton display/graphic to look like the down-arrow button in a QComboBox - and tips/code on how to overlay that graphic (especially if said graphic comes via a file) onto my own QPushButton.
More details, context:
I'm seeking to replace a QComboBox widget with a QLineEdit + QCalendarWidget, because QDateEdit isn't as customizable as I need (I think...). The thought is to place a QPushButton immediately adjacent (on the right-side) of the QLineEdit to make it things look like regular QComboBox as much as possible. Then said button will .exec_() the QCalendarWidget (which is technically wrapped by a QDialog).
Let me know if this doesn't make sense, and I can provide further or clarified context.

You can try a QToolButton with no text and the arrowType property set to Qt.DownArrow. eg: myqtoolbutton.setArrowType(Qt.DownArrow).

Related

How to make QPushButton editable text when one click on it?

Using Python 2.7 and PyQt4.
So I need a way to make a text of the QPushButton editable when click on it, like on QTextEdit.
There is no builtin way to edit a push button in the sense that you have a cursor and can type along.
Probably the easiest solution is to bring up a QInputDialog. If that feels to heavy, you could also place a floating QLineEdit over or next to the QPushButton. Close that on <Enter> and set the typed text to the QPushButton.
If you really want an editable Button, you'll have to subclass QPushButton and implement the desired functionality yourself. To get started with this, you need to reimplement mousePressEvent() for starting your editing mode. Reimplement keyPressEvent() for handling key strokes. If you need to display a cursor, reimplement paintEvent(). I have no particular resource at hand that describes what exactly you have to do, but the terms above should be sufficient to look it up yourself.

Automatic focus on showing deletes the placeholder text of a QLineEdit

I use PyQt4 and Python 2.7.9.
My program contains a few QLineEdit objects. The problem is that when the program is launched, one of the QLineEdits is being focused automatically, which causes my placeholder text to disappear.
Is there any way to prevent it, or at least don't let it hide the placeholder text?
Another way is
self.this_widget.clearFocus()
after window has been shown. Only in Qt5 placeholder texts are displayed even with focus. So maybe switch to PyQt5.
You can use setFocus to put the focus on a different widget (although, depending on which widget you pick, you might also need to set the focus-policy first):
self.some_other_widget.setFocusPolicy(QtCore.Qt.TabFocus)
self.some_other_widget.setFocus()
Alternatively, if you use Qt Designer to create the GUI, you could edit the tab-order so that the line-edit is not the first in the chain. This can also be done programmatically using QWidget.setTabOrder.

Set QButton height

I probably should post this in multiple questions.
I need to know two things, I have a simple window that all it has is a button within it, set like so:
self.spherize = QtGui.QPushButton("Spherize")
I'm just wondering how to set the height of this button?
I've tried the following but it didn't work:
self.spherize.setHeight(50)
Also, where can I find propper docs for things like this with example code?
This is PyQT documentation for QPushButton, but it inherits from QAbstractButton and QWidget which has the methods for changing widget size.
The call you want is QWidget.setFixedHeight. So you want something like:
self.spherize.setFixedHeight(50)
Here is the PySide documentation.

PyQt4 - Making a QIcon look active or "pressed down"

I have a QMainWindow with a toolbar that has a bunch of QIcon buttons. Clicking these buttons switches between widgets in a QStackedWidget. Basically, the buttons function as tabs, opening different parts of the program. Now, what I want to do is make a specific button look pressed down when the corresponding widget is active, so it's easier to see which part of the program is active. Like this:
I rephrased it 20 different ways while Googling and checked out the manual but I'm still at a loss. I'm probably missing something painfully obvious here.
Of course I was missing something painfully obvious :P Here's the solution:
# Create the button and make it checkable
self.testButton = QtGui.QAction(QtGui.QIcon('images/icons/test.png'), 'Test', self)
self.testButton.setCheckable(True)
# Mark the button as checked
self.testButton.setChecked(True)

Arranging pyqt combobox in toolbar

I have made a toolbar in qt designer with a few buttons. I have found some answers on stack that say you can not add a combobox in qt designer. With this I found an example of adding it manually. The method was:
self.combo=QtGui.QComboBox(self.toolBar)
self.combo=insertItems(1,["One","Two","Three"])
However, this puts the combobox all the way on the left ontop of my other buttons. How do I add this to the end? I read the doc that says the QComboBox is QStandardItemModel, which either takes self or a parent. I have tried giving extra arguments like some sort of index but the error says it only takes one argument. How can I specify which location the combobox will go?
Thanks
You added QComboBox as a child of QToolbar. It doesn't belong to any layout, so it doesn't take space in toolbar's layout. You need to use QToolbar::addWidget or QToolbar::insertWidget instead.
self.combo=QtGui.QComboBox()
toolBar.addWidget(self.combo)
self.combo.insertItems(1,["One","Two","Three"])
Note that I've replaced = to . in the last line. It should have been typing error.

Categories

Resources