How to remove the divider between widgets when using `statusBar.addPermanentWidget()`? - python

Is it possible to remove the divider line between two widgets that were added to the status bar using .addPermanentWidget()? I suspect that it is possible, but I haven't really found any literature on how to proceed.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QLabel
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
statusBar = QStatusBar()
self.setStatusBar(statusBar)
statusBar.addPermanentWidget(QLabel("Label: "))
statusBar.addPermanentWidget(QLabel("Data"))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

In order to remove the divider between the two elements you need to set the stylesheet for QStatusBar::item in either Qt Creator, or the project source.
Qt Creator Example:
Project Source Example:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QLabel
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
statusBar = QStatusBar()
statusBar.setStyleSheet('QStatusBar::item {border: None;}')
self.setStatusBar(statusBar)
statusBar.addPermanentWidget(QLabel("Label: "))
statusBar.addPermanentWidget(QLabel("Data"))
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

Another way is to combine several widgets into one to group them, something like the C++ below:
QWidget *widget = new QWidget;
QLayout* layout = new QHBoxLayout(widget);
layout->setMargin(0);
QLabel *label = new QLabel;
label->setText("Recording status");
layout->addWidget(label);
QLabel *m_RecordingStatus = new QLabel;
m_RecordingStatus->setFrameShape(QFrame::Shape::Box);
m_RecordingStatus->setFixedWidth(100);
layout->addWidget(m_RecordingStatus);
ui.m_statusBar->addPermanentWidget(widget);
You can group associated widgets to be together between dividers.

Related

Menu item now showing in PyQT6 GUI

I'm creating a basic pyqt6 application using the QMainWindow Class.
My code is basic. I create a basic window, but whenever I execute this code, I do not see any menu:
from PyQt6.QtWidgets import QStatusBar, QApplication, QWidget, QMainWindow, QVBoxLayout, QScrollBar, \
QToolButton
import sys
# There are THREE different window type classes
# that we can choose from:
# QWidget, QMainWindow, QDialog
class Window(QMainWindow):
def __init__(self):
super().__init__()
# Manages GUI Applications Control Flow
# and main settings..
app = QApplication([])
window = Window()
window.setWindowTitle("My 1st App")
window.statusBar().showMessage("Status Bar Message")
window.menuBar().addMenu("Menu 1")
window.show()
sys.exit(app.exec())
What am I doing wrong?
Why isn't "MENU 1" showing in the GUI?

A blank window after overriding PyQt5.QWidget.__init__()

I am learning to write PyQt5 codes.When I tried to code a subclass extented from PyQt5.QWidget and to override its constructor function, I found that the new code didn't work.(A blank window without anything you coded would show up.)It seems that the program stopped after "super().init()".Even I think the constructor don't run.But when I put the lines except "super()._init()" in a overridden function "PyQt5.Qwidegt.show()", it runs well.I am wondering why.It is quite pluzzing for a green hand.
The codes are as followed:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout,
QPushButton
class VBoxLayoutWindow(QWidget):
def __int__(self):
super().__init__()
self.resize(300, 300)
self.setWindowTitle("VBox Layout")
layout = QVBoxLayout()
button1 = QPushButton("Button1")
button2 = QPushButton("Button2")
button3 = QPushButton("Button3")
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(button3)
layout.addStretch(2)
self.setLayout(layout)
app = QApplication(sys.argv)
w = VBoxLayoutWindow()
w.show()
app.exec_()

How application structure should looks like?

I'm trying to create a small database app to keep all clients inside. I would like to write GUI using PyQt5. I have a problem with understanding how app structure should looks like.
I'd like to have a main class which starts the app and I want to seperate GUI, DB and Main classes in different files.
You can see my code snippets bellow. It don't work because some variables are not recognized and accually I don't understand why.
My thoughts:
1. Window, tab1 objects will be created in main class init function
2. When window, tab1 instances were created, the methods inside it's init will be called
3. I have window, tab1 objects and it's variables are available for themselves
window.gbT1Main.setLayout(T1LayMain) is not defined for TabNewClient class. Why ? How should I change my code to achieve above requirements? Please explain me how should I connect my classes :(
Window and TabNewClient class (window, tab1)
from PyQt5.QtWidgets import QApplication, QDialog, QTabWidget, QGroupBox, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QFormLayout, QLineEdit, QDateEdit, QTextEdit, QRadioButton, QGridLayout
import sys
import datetime
class Window(QDialog):
def __init__(self):
super().__init__()
self.InitWindow()
def InitWindow(self):
# create tab widget
self.tab = QTabWidget()
# create MainWindow groupbox
self.gbMainWindow = QGroupBox()
# TAB groupBoxes
self.gbT1Main = QGroupBox()
self.gbT2Main = QGroupBox("Main2")
self.gbT3Main = QGroupBox("Main3")
# Adding tabs
self.tab.addTab(self.gbT1Main, "Dodaj klienta")
self.tab.addTab(self.gbT2Main, "Wyszukaj")
self.tab.addTab(self.gbT3Main, "Statystki")
# Setting MainWindow title
self.setWindowTitle("MEDIKAP - gabinet medycyny pracy")
# Main Window Layout
self.layMainWindow = QHBoxLayout()
# Set MainWindow Layout
self.layMainWindow.addWidget(self.tab)
self.gbMainWindow.setLayout(self.layMainWindow)
# set MainWindow layout visible
self.setLayout(self.layMainWindow)
#show window
self.show()
class TabNewClient:
def __init__(self):
self.CreateTab1Layout()
def CreateTab1Layout(self):
self.gbAddClient = QGroupBox("Dane klienta")
self.gbRodzajBadania = QGroupBox("Podstawa prawna")
self.gbDane = QGroupBox()
self.gbComment = QGroupBox("Komentarz")
self.gbButtons = QGroupBox()
# TAB1 - layouts
T1LayMain = QVBoxLayout()
layDane = QHBoxLayout()
# TAB1
layDane.addWidget(self.gbAddClient)
layDane.addWidget(self.gbRodzajBadania)
self.gbDane.setLayout(layDane)
# TAB1 - set layout to Main
T1LayMain.addWidget(self.gbDane)
T1LayMain.addWidget(self.gbComment)
T1LayMain.addWidget(self.gbButtons)
window.gbT1Main.setLayout(T1LayMain)
Main class:
from PyQt5.QtWidgets import QApplication
import sys
from guiv3 import Window, TabNewClient
class Main:
def __init__(self):
window = Window()
tab1 = TabNewClient()
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Main()
app.exec_()
error:
window.gbT1Main.setLayout(T1LayMain)
NameError: name 'window' is not defined
To answer the question of why you get an error about 'window' not being defined, I believe it's because there is no window variable in the TabNewClient class. It looks like you're attempting to refer to the window defined in Main, but that won't work since that variable isn't in the scope of the TabNewClient class. My guess is that you're going to run into the same issue with gbT1Main, since that is outside the scope of the TabNewClient class as well.
Edit: I think I understand what you're attempting here. You want the gbT1Main related tab in window to hold the layout from tab1. To do that, you will need to set window's layout in Main:
from PyQt5.QtWidgets import QApplication
import sys
from guiv3 import Window, TabNewClient
class Main:
def __init__(self):
window = Window()
tab1 = TabNewClient()
window.gbT1Main.setLayout(tab1.T1LayMain)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = Main()
app.exec_()
That's most likely what you're looking for.
Important: this also requires that T1LayMain be an attribute of the TabNewClient class, so use self.T1LayMain inside that class for it to be accessible outside it.

pyqt5 / qtdesigner textbox default label

I am trying to create a GUI for my python program. One of the tools that I need is a text input box.
Now, I want a text label for this box saying "Please insert texts." Is there a function to add a label that shows inside the input textbox as default and disappear when user click the box to type?
I don't mind to use qt designer or pyqt5 coding.
Thank you guys.
placeholderText : QString
This property holds the line edit's placeholder text
import sys
from PyQt5.QtWidgets import QLineEdit, QVBoxLayout, QApplication, QWidget
class Test(QWidget):
def __init__(self):
super().__init__()
self.lineEdit = QLineEdit(placeholderText="Please insert texts.") # <---
vbox = QVBoxLayout(self)
vbox.addWidget(self.lineEdit)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())
I am begginer like you and my English is not so good. But I recommend you use Qt Designer. It's easier, fastter for you draw your app. I am using pyside2 project and recommend you read docummentatio each widgets you wanna use in PySide2 project and Qt Project. Try code below
enter image description here
import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QDialog
from PySide2.QtWidgets import QTextEdit
from PySide2.QtWidgets import QVBoxLayout
from PySide2.QtCore import Qt
class MainDialog(QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
# Create Widget TextEdit
self.text = QTextEdit()
# I think that you wanna this function in your program
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLineEdit.html?highlight=qlineedit#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.setPlaceholderText
# http://doc.qt.io/qt-5/qlineedit.html#placeholderText-prop
self.text.setPlaceholderText('''Yes! this is exactly what I want!
Thank you, what if you have a big text box (more than 10 lines) and
you want to scale up the place holder and align it in center?? ''')
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLineEdit.html?highlight=qlineedit#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.setAlignment
# http://doc.qt.io/qt-5/qlineedit.html#alignment-prop
self.text.setAlignment(Qt.AlignCenter)
# Layout
layout = QVBoxLayout()
layout.addWidget(self.text)
self.setLayout(layout)
def main():
app = QApplication()
mainDialog = MainDialog()
mainDialog.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

How do I make my QTableWidget class (any class) update/refresh itself?

I am making a gui using pyqt4. When I insert data from sqlite into my qtablewidget table, it will only update itself once I have closed the program and reopened it. How can I have the program update itself automatically (refresh the class) after inserting/deleting or changing the data in anyway?
Since I am not sure how you are implementing your code, maybe the following code helps you.
#!/usr/bin/python
import sys
from PyQt4.QtGui import QWidget, QPushButton, QMainWindow, QTableWidget,QTableWidgetItem, QVBoxLayout, QApplication
from PyQt4.QtCore import Qt
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
"""
"""
super(MyMainWindow,self).__init__(parent)
self.setWidgets()
def setWidgets(self, ):
vBox = QVBoxLayout()
mainFrame = QWidget()
self._pressButton = QPushButton("Update Table",self)
self._pressButton.clicked.connect(self.updateTable)
self._table = QTableWidget(self)
self._table.setRowCount(3)
self._table.setColumnCount(3)
vBox.addWidget(self._pressButton)
vBox.addWidget(self._table)
mainFrame.setLayout(vBox)
self.setCentralWidget(mainFrame)
def updateTable(self, ):
i = self._table.currentRow()
if i == -1:
i=0
self._table.insertRow(i)
self._table.setItem(i,0,QTableWidgetItem("Test"))
if __name__ == '__main__':
qApp = QApplication(sys.argv)
MainWindow = MyMainWindow()
MainWindow.show()
sys.exit(qApp.exec_())
Cheers

Categories