选项卡中的小部件放置

widgets placement in tabs

所以我有这个代码:

from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QHBoxLayout, QComboBox, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from lang import getLang
import sys, os, configparser

config = configparser.ConfigParser()
config.read("Settings.ini")

# Config get setting and change setting
#print(config.get("Main", "language"))
#config.set("Main", "language", "danish")
#with open("Settings.ini", "w") as cfg_file:
    #config.write(cfg_file)

class App(QMainWindow):
    def __init__(self):
        super().__init__()

        # Window Settings
        self.x, self.y, self.w, self.h = 0, 0, 300, 200

        self.setGeometry(self.x, self.y, self.w, self.h)

        self.window = MainWindow(self)
        self.setCentralWidget(self.window)
        self.setWindowTitle("Window title") # Window Title
        self.show()

class MainWindow(QWidget):        
    def __init__(self, parent):   
        super(QWidget, self).__init__(parent)
        layout = QVBoxLayout(self)

        # Run this after settings
        self.lang = getLang(config.get("Main", "language"))

        # Initialize tabs
        tab_holder = QTabWidget()   # Create tab holder
        tab_1 = QWidget()           # Tab one
        tab_2 = QWidget()           # Tab two

        # Add tabs
        tab_holder.addTab(tab_1, self.lang["tab_1_title"]) # Add "tab1" to the tabs holder "tabs"
        tab_holder.addTab(tab_2, self.lang["tab_2_title"]) # Add "tab2" to the tabs holder "tabs" 

        # Create first tab
        tab_1.layout = QVBoxLayout(self)
        tab_2.layout = QVBoxLayout(self)

        # Buttons
        button_start = QPushButton(self.lang["btn_start"])
        button_stop = QPushButton(self.lang["btn_stop"])
        button_test = QPushButton(self.lang["btn_test"])

        # Button Extra
        button_start.setToolTip("This is a tooltip for the button!")    # Message to show when mouse hover
        button_start.clicked.connect(self.on_click)

        button_stop.clicked.connect(self.on_click)

        button_test.clicked.connect(self.on_click)
        #button_start.setEnabled(False)

        # comboBox
        label_language = QLabel("Language")
        combo_language = QComboBox(self)
        combo_language.addItem(self.lang["language_danish"])
        combo_language.addItem(self.lang["language_english"])

        # Move widgets
        combo_language.move(50, 150)
        label_language.move(50, 50)

        # Tab Binding
        self.AddToTab(tab_1, button_start)
        self.AddToTab(tab_1, button_stop)
        self.AddToTab(tab_2, label_language)
        self.AddToTab(tab_2, combo_language)

        # Add tabs to widget        
        tab_1.setLayout(tab_1.layout)
        tab_2.setLayout(tab_2.layout)
        layout.addWidget(tab_holder)
        self.setLayout(layout)

    @pyqtSlot()
    def on_click(self):
        button = self.sender().text()
        if button == self.lang["btn_start"]:
            print("Dank")
        elif button == self.lang["btn_stop"]:
            print("Not dank")

    def AddToTab(self, tab, obj):
        tab.layout.addWidget(obj)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

目前它创建了一个 window,其中包含第一个选项卡上的 2 个按钮。 "Start" 和 "Stop" 按钮,除了打印文本外还没有实际功能。 在选项卡 2 上,我有一个标签 "Language:" 和一个包含 "Danish" 和 "English" 的下拉菜单。

我遇到的问题是,它的位置真的很奇怪而且很烦人,如下所示:

我不确定如何更改位置,因为我不能只在文本标签、按钮和下拉菜单上使用 .move,因为它们位于选项卡中。

例如,在选项卡二上,我希望标签 "Language:" 位于下拉菜单左侧的右侧。

问题是因为您正在使用 QVBoxLayout 在每个选项卡中设置小部件。布局的任务是管理小部件的位置和大小,所以你不能使用move(),此外也没有必要。

解决方案是为每个选项卡创建一个自定义布局,但保持顺序将为每个选项卡创建一个自定义小部件,如果是第一个选项卡,我将继续使用 QVBoxlayout 但添加拉伸,使小部件停留在顶部位置。在第二种情况下,我将 QVBoxLayoutQHBoxLayout 组合,QHBoxLayout 将使用它来放置 QLabelQComboBox 以及 QVBoxLayout 将使用它将小部件推到顶部位置。

执行上面的代码,出于明显的原因我省略了 lang,我们得到以下内容:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys, os, configparser

config = configparser.ConfigParser()
config.read("Settings.ini")

# Config get setting and change setting
#print(config.get("Main", "language"))
#config.set("Main", "language", "danish")
#with open("Settings.ini", "w") as cfg_file:
    #config.write(cfg_file)

class App(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        # Window Settings
        self.x, self.y, self.w, self.h = 0, 0, 300, 200
        self.setGeometry(self.x, self.y, self.w, self.h)

        self.window = MainWindow(self)
        self.setCentralWidget(self.window)
        self.setWindowTitle("Window title") # Window Title
        self.show()

class GeneralWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(GeneralWidget, self).__init__(parent)
        lay = QtWidgets.QVBoxLayout(self)
        # Buttons
        button_start = QtWidgets.QPushButton("start") #self.lang["btn_start"])
        button_stop = QtWidgets.QPushButton("stop") #self.lang["btn_stop"])

        # Button Extra
        button_start.setToolTip("This is a tooltip for the button!")    # Message to show when mouse hover
        button_start.clicked.connect(self.on_click)

        button_stop.clicked.connect(self.on_click)

        lay.addWidget(button_start)
        lay.addWidget(button_stop)
        lay.addStretch()

    @QtCore.pyqtSlot()
    def on_click(self):
        button = self.sender().text()
        if button == self.lang["btn_start"]:
            print("Dank")
        elif button == self.lang["btn_stop"]:
            print("Not dank")


class OptionsWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(OptionsWidget, self).__init__(parent)
        lay = QtWidgets.QVBoxLayout(self)
        hlay = QtWidgets.QHBoxLayout()
        lay.addLayout(hlay)
        lay.addStretch()

        label_language = QtWidgets.QLabel("Language")
        combo_language = QtWidgets.QComboBox(self)
        combo_language.addItem("item1") #self.lang["language_danish"])
        combo_language.addItem("item2") #self.lang["language_english"])
        hlay.addWidget(label_language)
        hlay.addWidget(combo_language)



class MainWindow(QtWidgets.QWidget):        
    def __init__(self, parent):   
        super(MainWindow, self).__init__(parent)
        layout = QtWidgets.QVBoxLayout(self)
        # Run this after settings
        # self.lang = getLang(config.get("Main", "language"))
        # Initialize tabs
        tab_holder = QtWidgets.QTabWidget()   # Create tab holder
        tab_1 = GeneralWidget()           # Tab one
        tab_2 = OptionsWidget()           # Tab two
        # Add tabs
        tab_holder.addTab(tab_1, "General") #self.lang["tab_1_title"]) # Add "tab1" to the tabs holder "tabs"
        tab_holder.addTab(tab_2, "Options") #self.lang["tab_2_title"]) # Add "tab2" to the tabs holder "tabs" 

        layout.addWidget(tab_holder)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())