I want a combobox with width adjusted to its (longest) content, so I use AdjustToContents. However, at least with my settings and the Oxygen style, I sometimes get a too short box and the contents are clipped. This seems to happen especially with digits:
Note that the 8 at the end is clipped. If I comment out that item, the length looks ok.
Is it a bug? Is there an easy way to fix it maybe with stylesheets or subclassing?
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import *
class HelloWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("Test")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QHBoxLayout()
centralWidget.setLayout(gridLayout)
label = QLabel("Name")
combobox = QComboBox()
combobox.setSizeAdjustPolicy(QComboBox.AdjustToContents)
combobox.addItem("Short")
combobox.addItem("Longer text")
combobox.addItem("abcdefghijklmnopqr")
combobox.addItem("123456789012345678")
gridLayout.addWidget(label)
gridLayout.addWidget(combobox)
gridLayout.addStretch(1)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = HelloWindow()
mainWin.show()
sys.exit( app.exec_() )
try:
combobox = QComboBox(self)
It seems I found the culprit. I had the "Force fonts DPI" option in KDE fonts settings enabled and set to 96 (while xdpyinfo | grep resolution says 101 dpi). Disabling the option makes it work fine with all styles I have available (Breeze, Oxygen, Windows, Fusion).
Now, is this a KDE bug, a Qt bug or just a limitation?
Related
I am currently developing an application in which i cannot use modal windows (due to some application constraints). However, in some cases i would like to simulate a popup window. To do so i dynamically create a widget that has the centralwidget as parent and i use the move() method to place it where i want.
I would like to know if there is a way to get a widget's dimensions at a given time (considering the mainWindow can be resized at any time) so that i will be able to center the placeholder popup (a simple widget) at the middle of the centralwidget.
Thank you
For getting Qt Widget size:
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QWidget()
width = mainWindow.frameGeometry().width()
height = mainWindow.frameGeometry().height()
For gettting screen size
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
mainWindow = QtGui.QWidget()
screenShape = QtGui.QDesktopWidget().screenGeometry()
mainWindow.resize(self.screenShape.width(), self.screenShape.height())
mainWindow.show()
You can use frameGeometry or geometry depending on your needs.
I've got a PyQt GUI with a QTextEdit in it. I have set a few of the widget settings to play with things like the font size. What I'm seeing is that when I initially type in the field, the settings are applied, but if I delete all text and start typing again, the settings have reset to the default ones. Below is a MWE where I can see this behavior. Just in case it matters, I'm using Python 3.5.1 with PyQt4 4.8.7.
from PyQt4 import QtCore, QtGui
class App(object):
def __init__(self):
self.app = QtGui.QApplication([]) # The main application
self.win = QtGui.QMainWindow() # The main window
self.widget = QtGui.QWidget() # The central widget in the main window
self.grid = QtGui.QVBoxLayout() # The layout manager of the central widget
self.textArea = QtGui.QTextEdit()
self.grid.addWidget(self.textArea)
self.textArea.setMinimumSize(600,300)
self.textArea.setLineWrapMode(QtGui.QTextEdit.NoWrap)
self.textArea.setFontPointSize(12)
self.widget.setLayout(self.grid)
self.win.setCentralWidget(self.widget)
self.win.show()
self.app.exec_()
App()
You can create a new QFont item and then you can use QTextEdit.setFont()
This way it will not reset after all text is deleted.
I am coding a GUI in PyQt5, where I need a color picker.
So far, I use the QColorDialog Class, which works fine for selecting a color - but my problem is that there seems to be no way to select "no color" (or "no fill", like its known from PowerPoint or Adobe Illustrator).
How to achieve to select "no color"? (The documentation only mentions a flag for transparency, but this is not helpful for me...)
If you don't mind using a non-native dialog, it is quite easy to customize it.
Below is a very basic implementation that shows how to embed the existing dialog, and add an extra "No Color" button at the bottom. The rest of the implementation is left as an exercise for the reader...
from PyQt5 import QtCore, QtWidgets
class ColorDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
widget = QtWidgets.QColorDialog()
widget.setWindowFlags(QtCore.Qt.Widget)
widget.setOptions(
QtWidgets.QColorDialog.DontUseNativeDialog |
QtWidgets.QColorDialog.NoButtons)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(widget)
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(QtWidgets.QPushButton('No Color'))
hbox.addWidget(QtWidgets.QPushButton('Cancel'))
hbox.addWidget(QtWidgets.QPushButton('Ok'))
layout.addLayout(hbox)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
dialog = ColorDialog()
dialog.show()
sys.exit(app.exec_())
Disclaimer: New to both python and qt designer
QT Designer 4.8.7
Python 3.4
PyCharm 5.0.3
Question - How do I add controls to the main form or a scroll area widget on the main form (created in QT Designer) programmatically?
I have created a MainWindow in qt designer and added my widgets. The following is the entire test program in PyCharm:
import sys
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
qtCreatorFile = "programLauncher.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
# Cannot resize or maximize
self.setFixedSize(1045, 770)
# Add button test
self.dateLabel = QtGui.QLabel("Test")
self.pushButton = QtGui.QPushButton('Test button')
# self.scrollArea_programs.addWidget()
grid = QtGui.QGridLayout()
# self.scrollArea_programs.addWidget(self.pushButton)
grid.addWidget(self.dateLabel,0,0)
grid.addWidget(self.pushButton,0,1)
self.setLayout(grid)
self.pushButton_exit.clicked.connect(self.closeEvent)
def closeEvent(self):
QtGui.QApplication.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
As you can see I tried to add controls to a grid but nothing shows up when the program runs - I have also tried to add a control to the scroll area. Can someone help me to just add 1 control to the scroll area at run time - so then I can know the proper way to do it or "a" proper way to do this.
Thanks in advance
Without having access to your programLauncher.ui and making minimal changes to your posted code, you can add your UI elements to the window like so:
from PyQt4 import QtGui
import sys
class MyApp(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
# Cannot resize or maximize
self.setFixedSize(1045, 770)
widget = QtGui.QWidget(self)
self.setCentralWidget(widget)
# Add button test
self.dateLabel = QtGui.QLabel("Test")
self.pushButton = QtGui.QPushButton('Test button')
grid = QtGui.QGridLayout()
grid.addWidget(self.dateLabel, 0, 0)
grid.addWidget(self.pushButton, 0, 1)
widget.setLayout(grid)
self.pushButton.clicked.connect(self.closeEvent)
def closeEvent(self, event):
QtGui.QApplication.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
This will get the controls on the screen, although the layout leaves a lot to be desired. You may have to make modifications to this based on what's in your .ui file. One thing that you'll want to note in this example is that the QMainWindow needs a central widget (widget in the example above). You then set the layout on that widget.
You can use the designer to create your .ui file
The you can load it in your .py using something like:
from PyQt4 import QtCore, QtGui, uic
class my_win(QtGui.QMainWindow):
def __init__(self):
self.ui = uic.loadUi('my_ui.ui',self)
then you can access all your widgets with something like
self.ui.actionQuit.triggered.connect(QtGui.qApp.quit)
or
self.ui.my_button.triggered.connect(self.do_someting)
Thanks to JCVanHamme (the programLauncher.ui hint) and also outside help I now learned most of what I need to know to access MainWindow at run time. So for anyone interested in this beginner tip:
Take a blank form in QT Designer
Add a control
Run pyuic4 batch file
Take a look at the generated .py file to learn EVERYTHING about how to add controls.
Don't let the power go to your head - cheers
This is a Mac OS QT issue,
I have created a QMainWindow and added a toolbar to it.
after setting "setUnifiedTitleAndToolBarOnMac" flag to TRUE my toolbar got unified to my mainwindow.
But when i customize the window buttons such as only close button option,
my window Toolbar got detached from title bar as if it looks like a winodow OS toolbar
not like a Mac native one.
Attached my test code below which is in pyqt.
Hope someone know how to achieve it.
Thanks
from PyQt4 import QtGui
from PyQt4 import QtCore
class Ui_windo(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setObjectName("windo")
self.resize(400, 300)
self.setWindowTitle(QtGui.QApplication.translate("window", "window", None, QtGui.QApplication.UnicodeUTF8))
self.b1 = QtGui.QToolButton()
self.b1.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
self.b1.setText('Test')
self.b1.setObjectName("b1")
self.b1.setCheckable(True)
_toolBar = self.addToolBar('test')
_toolBar.setMovable(False)
_toolBar.addWidget(self.b1)
self.setUnifiedTitleAndToolBarOnMac(True);
_windowButtons = QtCore.Qt.Window| QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.CustomizeWindowHint
self.setWindowFlags(_windowButtons)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_windo()
ui.show()
sys.exit(app.exec_())
Seems that the Qt.CustomizeWindowHint flag removes the styling from the toolbar. Not sure if this is expected behavior or a bug. There have been a number of style-related bug reports pertaining to setUnifiedTitleAndToolBarOnMac. You should post it to confirm whether its expected or not.
https://bugreports.qt-project.org/secure/IssueNavigator.jspa
If you set your window flag to use Qt.Tool, that will at least get you close by removing the minimize button.