I have my qtablewidget defined like this:
def __init__(self, parent = None):
super(Window, self).__init__(parent)
QtGui.QWidget.__init__(self)
QtGui.QTableWidget.setMinimumSize(self, 500, 500)
QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
self.table = QtGui.QTableWidget()
rowf = 3
self.table.setColumnCount(3)
self.table.setRowCount(rowf)
self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
self.table.verticalHeader().hide()
header = self.table.horizontalHeader()
header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(self.table)
self.verticalLayout.addWidget(self.buttonBox)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)
I would like my end result to look something similar to the pic below but right now, the layout that I'm trying to add doesn't quiet work the way I'd like it to. I'm a beginner at pyqt. I've tried this layout before on a qlistview and it worked well.
add {your table}.table.horizontalHeader().setStretchLastSection(True) and/or {your table}.verticalHeader().setStretchLastSection(True)
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
QtGui.QTableWidget.setMinimumSize(self, 500, 500)
QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
self.table = QtGui.QTableWidget()
rowf = 3
self.table.setColumnCount(3)
self.table.setRowCount(rowf)
self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
self.table.horizontalHeader().setStretchLastSection(True)
# self.table.verticalHeader().setStretchLastSection(True)
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(self.table)
self.verticalLayout.addWidget(self.buttonBox)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Only Horizontal:
Only Vertical:
Vertical and Horizontal:
Related
Is there a way to define the menubar in a QtabWidget class ?
I wrote a code for a destopapplication with pyqt5 and python 3.6. I would like to set the manuBar in the same class as the Tabs but my code returns qtabwidget has no attribute QMainWindow.
Here is my code:
import sys
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
from PyQt5.QtWidgets import *
class main_window(QTabWidget):
def __init__(self, parent=None):
super(QTabWidget, self).__init__(parent)
self.setGeometry(50, 50, 1100, 750)
self.setWindowTitle("Programm") #
self.centralWidget = QtWidgets.QWidget()
self.tabWidget = QtWidgets.QTabWidget(self.centralWidget)
self.tabWidget.setGeometry(QtCore.QRect(10, 10, 1200, 1000))
open_new_file = QAction('New', self)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('Projekt')
fileMenu.addAction(open_new_file)
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.show()
self.tab_v1 = QtWidgets.QWidget()
self.addTab(self.tab_v1, "Tab 1")
self.tab_v2 = QtWidgets.QWidget()
self.addTab(self.tab_v2, "Tab 2")
self.openFile = QPushButton("Choose Tab ", self.tab_v1)
self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))
def main():
app = QApplication(sys.argv)
ex = main_window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
QTabWidget can not have a QMenuBar, what you have to do is put the centralwidget of a QMainWindow to the QTabWidget.
import sys
from PyQt5 import QtCore, QtWidgets
class Main_window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Main_window, self).__init__(parent)
self.setGeometry(50, 50, 1100, 750)
self.setWindowTitle("Programm")
open_new_file = QtWidgets.QAction('New', self)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('Projekt')
fileMenu.addAction(open_new_file)
self.tabWidget = QtWidgets.QTabWidget()
self.setCentralWidget(self.tabWidget)
self.tab_v1 = QtWidgets.QWidget()
self.tabWidget.addTab(self.tab_v1, "Tab 1")
self.openFile =QtWidgets.QPushButton("Choose Tab ", self.tab_v1)
self.openFile.setGeometry(QtCore.QRect(700, 25, 200, 30))
self.tab_v2 = QtWidgets.QWidget()
self.tabWidget.addTab(self.tab_v2, "Tab 2")
def main():
app = QtWidgets.QApplication(sys.argv)
ex = Main_window()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I'm new to python and pyqt. I'm trying to open a new window after the first screen. My second window opens but without the options I specified, label and pushbutton.
from PyQt5 import QtWidgets
import sys
class secondwindow(QtWidgets.QMainWindow):
def __init__(self):
super(secondwindow, self).__init__()
self.label1 = QtWidgets.QLabel("Second Window");
self.button1 = QtWidgets.QPushButton("Click Me");
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(self.label1)
hbox.addWidget(self.button1)
self.setLayout(hbox)
class Window(QtWidgets.QWidget):
def btnclicked(self):
sender = self.sender()
if sender.text() == "OK":
self.secwin.show()
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.button1 = QtWidgets.QPushButton("OK");
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(self.button1)
self.setLayout(vbox)
self.button1.clicked.connect(self.btnclicked)
self.secwin = secondwindow()
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
main = Window()
main.show
sys.exit(app.exec())
if __name__ == '__main__':
main()
QMainWindow is a special widget because it has a defined structure, http://doc.qt.io/qt-5/qmainwindow.html#qt-main-window-framework:
As shown in the image there is already an area destined to place the widgets called Central Widget, in it you must place the widgets that you want to be displayed for it, you use setCentralWidget().
In your case the solution is:
class secondwindow(QtWidgets.QMainWindow):
def __init__(self):
super(secondwindow, self).__init__()
central_widget = QtWidgets.QWidget()
self.label1 = QtWidgets.QLabel("Second Window")
self.button1 = QtWidgets.QPushButton("Click Me")
hbox = QtWidgets.QHBoxLayout(central_widget)
hbox.addWidget(self.label1)
hbox.addWidget(self.button1)
self.setCentralWidget(central_widget)
I've already asked a similar question here but I'd like to know the proper way to add a layout to qtablewigets and also how could I put 2 table widgets in the same window, side by side if they both only had 3 columns.
Place the tables them within a QHBoxLayout.
Code:
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
QtGui.QTableWidget.setMinimumSize(self, 500, 500)
QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
self.table1 = QtGui.QTableWidget()
self.configureTable(self.table1)
self.table2 = QtGui.QTableWidget()
self.configureTable(self.table2)
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.addWidget(self.table1)
self.horizontalLayout.addWidget(self.table2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.verticalLayout.addWidget(self.buttonBox)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)
def configureTable(self, table):
rowf = 3
table.setColumnCount(3)
table.setRowCount(rowf)
table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
table.horizontalHeader().setStretchLastSection(True)
# table.verticalHeader().setStretchLastSection(True)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
Images:
I have written the code below:
import sys
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
Login_Widget = LoginPage(self)
self.central_widget.addWidget(Login_Widget)
self.central_widget.setCurrentWidget(Login_Widget)
self.setStyleSheet("background-color:#FFDA00;")
class LoginPage(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginPage, self).__init__(parent)
self.Username = QtGui.QLineEdit(self)
self.Password = QtGui.QLineEdit(self)
self.Password.setEchoMode(QtGui.QLineEdit.Password)
self.buttonLogin = QtGui.QPushButton('Login', self)
self.cancelButton = QtGui.QPushButton('Cancel', self)
loginLayout = QtGui.QFormLayout()
loginLayout.addRow("Username", self.Username)
loginLayout.addRow("Password", self.Password)
horizontallayout = QtGui.QHBoxLayout()
horizontallayout.addWidget(self.buttonLogin)
horizontallayout.addWidget(self.cancelButton)
layout = QtGui.QVBoxLayout(self)
layout.addLayout(loginLayout)
layout.addLayout(horizontallayout)
self.setLayout(layout)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
With the code above, When I run the code, it sets everything orange. I wanted to set the background orange, the lineedit white and the buttons silvery-grey. How would I set different colors for individual widget items?? Also is there any way that I could set a colour for the Window bar (the bar containing the window-title, exit button, minimize button, and re-size button)
Any help will be much appreciated!
You can set different style properties for individual widgets.
Have a look at this link Qt Style Sheets Examples as it cover how to set different style properties for most of the widgets.
You can also save the stylesheeet as .qss file and save it externally.
css = '''
QMainWindow
{
background:orange;
}
QLineEdit
{
background:white;
}
QPushButton
{
background:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #FAFFFA, stop: 0.4 #F5F7F5,
stop: 0.5 #F0F2F0, stop: 1.0 #EDEDED);
}
'''
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
Login_Widget = LoginPage(self)
self.central_widget.addWidget(Login_Widget)
self.central_widget.setCurrentWidget(Login_Widget)
class LoginPage(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginPage, self).__init__(parent)
self.Username = QtGui.QLineEdit(self)
self.Password = QtGui.QLineEdit(self)
self.Password.setEchoMode(QtGui.QLineEdit.Password)
self.buttonLogin = QtGui.QPushButton('Login', self)
self.cancelButton = QtGui.QPushButton('Cancel', self)
loginLayout = QtGui.QFormLayout()
loginLayout.addRow("Username", self.Username)
loginLayout.addRow("Password", self.Password)
horizontallayout = QtGui.QHBoxLayout()
horizontallayout.addWidget(self.buttonLogin)
horizontallayout.addWidget(self.cancelButton)
layout = QtGui.QVBoxLayout(self)
layout.addLayout(loginLayout)
layout.addLayout(horizontallayout)
self.setLayout(layout)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyleSheet(css)#<------------set your stylesheet
window = MainWindow()
window.show()
app.exec_()
As for the window title bar, it is not possible to set color or any properties. Your best bet is to hide it and implement your own window title bar.
I've created a class, which creates the GUI. I would like to add a menubar to it, but I don't really know, how should I add it to the window, if I work with a class. I can't make the menu bar appaer.
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
Main = QtGui.QMainWindow()
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.tempLabel=QtGui.QLabel("NC",self)
self.tempLabel.move(350,20)
self.tempLabel.setStyleSheet('color: black; font-size: 12pt;font: bold')
#menu bar
self.menu=QtGui.QMenu("Port", self)
self.menu.addAction('&ttyUSB0',)
self.menu.addAction('&ttyUSB1',)
self.menu.addAction('&ttyUSB2',)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.canvas)
self.layout.addWidget(self.tempLabel)
self.tab1.setLayout(self.layout)
self.tabs = QtGui.QTabWidget()
self.tabs.addTab(self.tab1, "Database")
self.tabs.addTab(self.tab2, "Current")
self.tabs.show()
The menu bar is usually accessed from the main window, using the menuBar function.
I have edited your example code to show how to add menus, and also fixed a few other minor issues:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
menubar = self.menuBar()
menu = menubar.addMenu('Port')
menu.addAction('&ttyUSB0')
menu.addAction('&ttyUSB1')
menu.addAction('&ttyUSB2')
self.tab1 = QtGui.QWidget()
self.tab2 = QtGui.QWidget()
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.tempLabel = QtGui.QLabel('NC', self)
self.tempLabel.move(350, 20)
self.tempLabel.setStyleSheet(
'color: black; font-size: 12pt;font: bold')
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.canvas)
self.layout.addWidget(self.tempLabel)
self.tab1.setLayout(self.layout)
self.tabs = QtGui.QTabWidget()
self.tabs.addTab(self.tab1, 'Database')
self.tabs.addTab(self.tab2, 'Current')
self.setCentralWidget(self.tabs)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())