working with the QTabWidget class, placing tabs
There is a panel with tabs (there are only three), how to make tab number 3 on top like here:
I would be very grateful
Code:
from PyQt5 import QtCore, QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("Класс QTabWidget")
window.resize(400, 150)
tab = QtWidgets.QTabWidget()
tab.addTab(QtWidgets.QLabel("Содержимое вкладки 1"),
"Вкладка 1")
tab.addTab(QtWidgets.QLabel("Содержимое вкладки 2"),
"Вкладка 2")
tab.addTab(QtWidgets.QLabel("Содержимое вкладки 3"),
"Вкладка 3")
tab.setElideMode(QtCore.Qt.ElideLeft)
tab.setCurrentIndex(0)
vbox = QtWidgets.QVBoxLayout()
vbox.addWidget(tab)
window.setLayout(vbox)
window.show()
sys.exit(app.exec_())
0
Author: Aleksandr Silkin, 2020-07-04
1 answers
As an option:
from PyQt5.Qt import *
class TabBar(QTabBar):
def tabSizeHint(self, index):
size = QTabBar.tabSizeHint(self, index)
w = int(self.width()/self.count())
return QSize(w, size.height())
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.tabwidget = QTabWidget()
self.tabwidget.setTabBar(TabBar())
self.tabwidget.setMovable(True)
self.tabwidget.setDocumentMode(True)
self.tabwidget.setElideMode(Qt.ElideRight)
self.tabwidget.setUsesScrollButtons(True)
self.setCentralWidget(self.tabwidget)
for name in ("Имя компьютера", "Оборудование"):
self.create_widgets(name)
def create_widgets(self, name):
w = QMainWindow()
self.tabwidget.addTab(w, name)
tab = QTabWidget()
tab.setTabBar(TabBar())
tab.setMovable(True)
tab.setDocumentMode(True)
tab.setElideMode(Qt.ElideRight)
tab.setUsesScrollButtons(True)
w.setCentralWidget(tab)
for i in ["Дополнительно", "Защита системы", "Удаленный доступ"]: # range(5):
tab.addTab(QWidget(), "{}".format(i))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setFont(QFont("Times", 12, QFont.Bold))
w = MainWindow()
w.resize(700, 200)
w.show()
sys.exit(app.exec_())
0
Author: S. Nick, 2020-07-04 13:13:38