PyQt格式下拉列表

时间:2017-11-23 05:19:11

标签: python sqlite pyqt pyqt5

我正在尝试用几个元素创建一个pyqt gui,我不确定我应该如何格式化它。基本上我想在页面顶部有三个下拉菜单按钮,当选择一个选项时,它会显示带有qtableview小部件的sqlite3数据库表。有关如何格式化下拉的任何帮助? (或者通常用于格式化pyqt的任何好资源?)谢谢!

编辑:这是我尝试过的:

    self.com_btn = QComboBox('Com')
    self.sec_btn = QComboBox('Sec')
    self.sto_btn = QComboBox('Sto')

    self.hbox = QHBoxLayout()
    self.hbox.addStretch(1)
    self.hbox.addWidget(self.com_btn)
    self.hbox.addWidget(self.sec_btn)
    self.hbox.addWidget(self.sto_btn)
    self.setLayout(self.hbox)

编辑:图片

enter image description here

2 个答案:

答案 0 :(得分:2)

要获得简单的单线效果,您可以像这样设置样式:

self.com_btn.setStyleSheet('QComboBox {border: 1px solid gray;}')

或者您可以制作一个样式表文件,然后通过主要功能将其添加到您的应用中:

if __name__ == '__main__':

    app = QApplication(sys.argv)
    css = open('path/to/my.css', 'r').read()
    app.setStyleSheet(css)
    mainWindow = Window()
    mainWindow.show()
    sys.exit(app.exec_())

您可以通过多种方式设置QComboBox的样式:

QComboBox {
    border: 1px solid gray;
    border-radius: 3px;
    padding: 1px 18px 1px 3px;
    min-width: 6em;
}

QComboBox:editable {
    background: white;
}

QComboBox:!editable, QComboBox::drop-down:editable {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
}

有关更多信息,您可以查看官方文档: https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox

答案 1 :(得分:0)

对于设计,我们发现QComboBox位于同一行,因此使用QHBoxLayout(),然后我们将该布局与QTableView放在一起QVBoxLayout()

要将表格链接到QComboBox,请使用模型QSqlTableModel,其中指明了表格的名称,以及必须使用setModel()显示的列,setTable()setModelColumn()

from PyQt5.QtWidgets import *
from PyQt5.QtSql import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        vlay = QVBoxLayout(self)
        hlay = QHBoxLayout()

        self.tv = QTableView(self)
        self.com_btn = QComboBox(self)
        self.sec_btn = QComboBox(self)
        self.sto_btn = QComboBox(self)
        hlay.addWidget(self.com_btn)
        hlay.addWidget(self.sec_btn)
        hlay.addWidget(self.sto_btn)

        model1 = QSqlTableModel(self)
        model1.setTable("table0")
        model1.select()
        self.com_btn.setModel(model1)
        self.com_btn.setModelColumn(2)

        model2 = QSqlTableModel(self)
        model2.setTable("table1")
        model2.select()
        self.sec_btn.setModel(model2)
        self.sec_btn.setModelColumn(1)

        model3 = QSqlTableModel(self)
        model3.setTable("table2")
        model3.select()
        self.sto_btn.setModel(model3)
        self.sto_btn.setModelColumn(0)

        vlay.addLayout(hlay)
        vlay.addWidget(self.tv)

def createConnection():
    db = QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName(':memory:')
    if not db.open():
        QMessageBox.critical(None, "Cannot open database",
                "Unable to establish a database connection.\n"
                "This example needs SQLite support. Please read the Qt SQL "
                "driver documentation for information how to build it.\n\n"
                "Click Cancel to exit.",
                QMessageBox.Cancel)
        return False

    query = QSqlQuery()
    for i in range(3):
        query.exec_("create table table{i} (field{i}_0, field{i}_1 varchar(20), field{i}_2 varchar(20))".format(i=i))
        for j in range(10):
            query.exec_("insert into table{i} values('value{i}{j}', 'value{i}{j}', 'value{i}{j}')".format(i=i, j=j))
    return True



if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)

    w = Widget()
    w.show()

    sys.exit(app.exec_())