Pyqt5 make QSplitter handle colorful/distinguishable - python

I want to change the way my splitter handle looks in Pyqt5. In the code below, consider self.widg_1 and self.widg_2 to be any two widgets. Using setStyleSheet results in the error:
self.my_splitter_handle.setStyleSheet("border: 3px blue")
AttributeError: 'NoneType' object has no attribute 'setStyleSheet'
import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QFrame, QLineEdit, QHBoxLayout, QSplitter
class MyMainWindow(QWidget):
def __init__(self):
super().__init__()
self.title = "PyQt5 Splitter"
self.top = 200
self.left = 500
self.width = 400
self.height = 300
hbox = QHBoxLayout()
self.widg_1 = QFrame()
self.widg_1.setFrameShape(QFrame.StyledPanel)
self.my_splitter = QSplitter(Qt.Horizontal)
self.widg_2 = QLineEdit()
self.my_splitter.addWidget(self.widg_1)
self.my_splitter.addWidget(self.widg_2)
self.my_splitter.setSizes([200, 200])
hbox.addWidget(self.my_splitter)
# Note: splitter handle 0 is always hidden and handle 1 is between the widgets 1 & 2
self.my_splitter_handle = self.my_splitter.handle(1)
self.my_splitter_handle.setStyleSheet("background: 3px blue;")
self.setLayout(hbox)
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.show()
App = QApplication(sys.argv)
window = MyMainWindow()
sys.exit(App.exec())
Note: I just want a decent and prominent looking splitter handle with an arrow or some other marker like the one in the image.

Related

How to make the size of the last column of QTableWidget equal to the others

I wanted to make a table that the size (width and height) of items inside the list to be equal. But the width of the last column is not the same as the others (it has been stretched)
I've used the setFixedSize to change the size, maybe that's the problem.
See this picture:
This is my code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QHeaderView, QTableWidget, QWidget
# Main Window
class App(QWidget):
def __init__(self):
super().__init__()
self.title = "PyQt5 - QTableWidget"
self.left = 200
self.top = 100
self.width = 740
self.height = 880
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
def createTable(self):
self.tableWidget = QTableWidget(self)
self.tableWidget.setRowCount(8)
self.tableWidget.setColumnCount(8)
self.tableWidget.setFixedSize(700, 700)
self.tableWidget.move(100, 100)
self.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.verticalHeader().setStretchLastSection(True)
self.tableWidget.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
How can I fix it?

Python using QGridLayout not add widget

I'm new to Qt5, I have a simple QGridLayout layout mask .
I want to create a windows with the widget resize with resize of window
this is the code
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog ,QVBoxLayout,QGroupBox,QGridLayout
class MainWindow(QtWidgets.QMainWindow, QtWidgets.QFileDialog, QtWidgets.QLineEdit):
def __init__(self):
super().__init__()
self.title = "Calcolo Hash"
self.top = 100
self.left = 100
self.width = 800
self.height = 330
self.InitWindow()
def InitWindow(self):
self.setWindowIcon(QtGui.QIcon("icona_aprie.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.creamaschera()
self.show()
def creamaschera(self):
print ("creazione maschera")
layout = QtWidgets.QGridLayout()
self.txtcartella = QtWidgets.QLineEdit()
self.lblprova = QtWidgets.QLabel("Please enter new name:")
# self.txtcartella.setGeometry(QtCore.QRect(10, 10, 301, 20))
# self.txtcartella.setObjectName("txtcartella")
layout.addWidget(self.lblprova,0,0)
layout.addWidget(self.txtcartella,0,1)
self.setLayout(layout)
# self.horizontalGroupBox.setLayout(layout)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
#w.show()
sys.exit(app.exec_())
but when I run the mask is empy.
I make the base with Qt5 designer and convert it to python. I want to refactor the class in a best workout.
Where is the error?
You should setLayout in a widget rather than setting it to the MainWindow since you are using the MainWindow class itself and while accessing the methods and properties of the class MainWindow you can be more specific
import sys
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import (QLineEdit, QMainWindow, QWidget,
QGridLayout, QLabel, QApplication)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.title = "Calcolo Hash"
self.top = 100
self.left = 100
self.width = 800
self.height = 330
self.InitWindow()
def InitWindow(self):
self.setWindowIcon(QtGui.QIcon("icona_aprie.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.creamaschera()
def creamaschera(self):
print("creazione maschera")
Layout = QGridLayout()
self.txtcartella = QLineEdit()
self.lblprova = QLabel("Enter Your Name")
self.lblprova.setGeometry(QtCore.QRect(15, 15, 301, 20))
self.txtcartella.setObjectName("txtcartella")
Layout.addWidget(self.lblprova, 0, 0)
Layout.addWidget(self.txtcartella, 0, 1)
# Widget to setLayout in it since you are using MainWindow as an Class
widget = QWidget()
widget.setLayout(Layout)
# SetCentralWidget without this widget won't be placed
self.setCentralWidget(widget)
# self.horizontalGroupBox.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())

QPlainTextEdit is RightToLeft but displays LeftToRight

I made a view with QPlainTextEdit and set setLayoutDirection(QtCore.Qt.RightToLeft). The output of self.plaintxt.isRightToLeft()is 1 but in the plain text view, persian and english text are displayed from left. What is happen in my code?
Code:
import sys, re
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QPushButton, QFileSystemModel, QTreeView, \
QFileDialog, QComboBox, QPlainTextEdit
from PyQt5.QtCore import pyqtSlot
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
self.title = 'by PyQt5 and python 3.7'
self.left = 10
self.top = 10
self.width = 1000
self.height = 500
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.btn_browse = QPushButton('Browse', self)
self.btn_browse.move(50, 20)
self.btn_browse.clicked.connect(self.on_click)
self.textbox = QLineEdit(self)
self.textbox.move(170, 20)
self.textbox.resize(280, 40)
self.textbox.setAlignment(QtCore.Qt.AlignRight) # It is in right.
self.page_view = QPlainTextEdit(self)
self.page_view.move(20, 100)
self.page_view.resize(800, 400)
self.page_view.setLayoutDirection(QtCore.Qt.RightToLeft) # It is not in right.
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
QWidget.setLayoutDirection no longer affects the text layout direction (Qt.LeftToRight or Qt.RightToLeft) of QTextEdit, QLineEdit and widgets based on them.
To programmatically force the text direction , you can change the defaultTextOption of the QTextDocument associated with that widget with a new QTextOption of different textDirection property.
QTextDocument *QPlainTextEdit::document() const
Returns a pointer to
the underlying document.
void QTextDocument::setDefaultTextOption(const QTextOption &option)
Sets the default text option to option.
self.page_view.document().setDefaultTextOption(QTextOption(Qt.AlignRight))
Don't use QPlainText, as it uses internally QPlainTextDocumentLayout which does not fully support RTL (automatic alignment for example - like you are looking for). You can use QTextEdit, or use a different document layout class (like QTextDocumentLayout) in your QPlainText.
The reason for those Plain classes exist, is to be faster by removing features... which you need.

widget position and window size

I am new to GUI development and I am trying to learn how to use pyqt5 in python.
Below is the example code I am working on. I want a window with some checkboxes,combobox and radiobuttons on the right side of the window. The details of the QtWidget objects are in the code.
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget, QPushButton, QComboBox, QRadioButton, QVBoxLayout, QCheckBox
from PyQt5.QtGui import QIcon
class App(QMainWindow):
def __init__(self):
super().__init__()
self.left = 0
self.top = 500
self.title = 'Chip2 Torque Data'
self.width = 500
self.height =500
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
xselect=QRadioButton("X",self)
xselect.setChecked(True)
xselect.move(340,400)
zselect=QRadioButton("Z",self)
zselect.move(380,400)
sselect=QRadioButton("SP1",self)
sselect.move(420,400)
pass_list=QComboBox(self)
pass_list.addItems(sheets_idealcut)
pass_list.move(340,300)
rawdata_check=QCheckBox("Raw Data",self)
rawdata_check.setChecked(True)
rawdata_check.move(340,200)
mvgavg_check=QCheckBox("Moving average",self)
mvgavg_check.setChecked(True)
mvgavg_check.move(380,200)
mvgstd_check=QCheckBox("Moving stdev",self)
mvgstd_check.setChecked(True)
mvgstd_check.move(420,200)
self.show()
if __name__ == '__main__':
  sheets_idealcut=['pass2','pass3','pass4','pass5']
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
else:
print('QApplication instance already exists: %s' % str(app))
ex = App()
#ex.show()
app.exec_()
Below is the output of the code. (Please ignore the overlapping checkbox names. I intend to correct it later.)
My problem is when I maximise this window, the QtWidget objects(QComboBox,QRadioButton,QCheckBox) don't readjust their position with the new window size.
So for the widgets to reposition themselves automatically, what method should be used ?
I googled it but I couldn't find anything useful.
Please help.
You are using move(x,y) which sets the Widgets to a specific fixed position. So you could write a function which is called on maximizing the window and then reposition your widgets accordingly.
Another automated approach is to use box layouts. QtGui.QHBoxLayout and QtGui.QVBoxLayout are basic layout classes which lines up widgets horizontally or vertically. From your pictures I do not know what exact positions you want your widgets to end up, so I try to explain it like I think you want it.
Each row of elements can be assigned into a HBox like this:
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(rawdata_check)
hbox.addWidget(mvgavg_check)
hbox.addWidget(mvgstd_check)
If you then resize the window the widgets should automaticly be repositioned to the right side of the window. The QtDesigner can help you arranging all the elements.
You should be using layouts for dynamic sizing. There are alot of different layouts. The layouts used in this example is the most basic types. I also changed the parent of "App" to a QDialog. They are easier to handle.
Here is an example of your app, using layouts:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget, QPushButton, QComboBox, QRadioButton, \
QVBoxLayout, QCheckBox, QHBoxLayout, QGroupBox, QDialog
from PyQt5.QtGui import QIcon
class App(QDialog):
def __init__(self):
super().__init__()
self.left = 0
self.top = 500
self.title = 'Chip2 Torque Data'
self.width = 500
self.height = 500
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.main_layout = QVBoxLayout()
xselect=QRadioButton("X",self)
xselect.setChecked(True)
# xselect.move(340,400)
self.main_layout.addWidget(xselect)
zselect=QRadioButton("Z",self)
# zselect.move(380,400)
self.main_layout.addWidget(zselect)
sselect=QRadioButton("SP1",self)
# sselect.move(420,400)
self.main_layout.addWidget(sselect)
pass_list=QComboBox(self)
pass_list.addItems(sheets_idealcut)
self.main_layout.addWidget(pass_list)
#pass_list.move(340,300)
rawdata_check=QCheckBox("Raw Data",self)
rawdata_check.setChecked(True)
self.main_layout.addWidget(rawdata_check)
#rawdata_check.move(340,200)
mvgavg_check=QCheckBox("Moving average",self)
mvgavg_check.setChecked(True)
#mvgavg_check.move(380,200)
mvgstd_check=QCheckBox("Moving stdev",self)
mvgstd_check.setChecked(True)
#mvgstd_check.move(420,200)
self.check_group = QHBoxLayout()
self.check_group.addWidget(mvgavg_check)
self.check_group.addWidget(mvgstd_check)
self.check_group.stretch(1)
self.main_layout.addLayout(self.check_group)
self.setLayout(self.main_layout)
self.show()
if __name__ == '__main__':
sheets_idealcut=['pass2','pass3','pass4','pass5']
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
else:
print('QApplication instance already exists: %s' % str(app))
ex = App()
#ex.show()
app.exec_()

PyQt: Changing cursor when hovering a button

I'm trying to make a button (or any other Qwidget), That will change users cursor when hovered.
So for instance, when i hover QPushButton, it will change cursor from Arrow to Pointing Hand.
I am using Qt Style Sheet, so i'm not entirely sure, but is there any way to do something like that there?, should look something like this:
btn.setStyleSheet("#btn {background-image: url(':/images/Button1.png'); border: none; }"
"#btn:hover { change-cursor: cursor('PointingHand'); }
Note: Code above is for example, second line will have no functionality at all.
However, if not, if there any other way i can achieve this?
For anyone who wants to achieve this in PyQt5, this is how I managed to do it. Let's say you have a button and you want your cursor to change to the 'PointingHandCursor' when you hover over the button.
You can do it with your_button.setCursor(QCursor(QtCore.Qt.PointingHandCursor)). For example:
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QLabel, QProgressBar,
QLineEdit, QFileDialog
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QCursor
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.title = "your_title"
self.screen_dim = (1600, 900)
self.width = 650
self.height = 400
self.left = int(self.screen_dim[0]/2 - self.width/2)
self.top = int(self.screen_dim[1]/2 - self.height/2)
self.init_window()
def init_window(self):
self.setWindowIcon(QtGui.QIcon('path_to_icon.png'))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setStyleSheet('background-color: rgb(52, 50, 51);')
self.create_layout()
self.show()
def create_layout(self):
self.button = QPushButton('Click Me', self)
self.button.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Use QWidget.setCursor (self, QCursor)
http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setCursor
http://doc.qt.io/qt-4.8/qwidget.html#cursor-prop
For Pyqt6 you can use this code:
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QCursor
closeBtn = QPushButton("X")
closeBtn.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))

Categories