Qt中给不同的控件赋予连接信号且生成响应

文章目录

  • 前言
    • 1. push Button控件
    • 2. line Edit控件,text Edit控件与push Button控件
    • 3. Combo Box控件,text Edit控件与push Button控件
    • 4. Tree Widget控件(比较综合)
    • 5. TableWidget控件与push Button控件结合(比较综合)
    • 6. TableWidget控件与push Button控件结合(比较综合)


前言

提示:原创不易,转载请引用连接

本篇文章乃是经过我长期学习Qt控件所积累的知识,制作前端就一定能用到,在这做个总结。


1. push Button控件

我们想要点击某一按钮,进而在界面可以发生某种反应。

self.pushButton.clicked.connect(self.load)

2. line Edit控件,text Edit控件与push Button控件

在控件中输入某一信息,点击按钮,进而可以在text Edit控件中显示所要查询的数据信息(前提是已经建立一张数据库信息,见前篇文章)

yousearch_content = self.lineEdit.text()             注:将按钮信号与line Edit控件连接起来,至关重要
if yousearch_content == "数据信息":
        cursor.execute('select * from awardList')      注:数据库表格中的第一行
        result = cursor.fetchone()
elif yousearch_content == "数据信息":                  注:数据库表格中的第二行
        cursor.execute('select * from awardList limit 1,1')
        result = cursor.fetchone()
elif yousearch_content == "数据信息":                  注:数据库表格中的第三行
        cursor.execute('select * from awardList limit 2,1')
        result = cursor.fetchone()
...
else:
        result = "无查询信息!" 
print(result)
self.plainTextEdit.setPlainText(str(result))
或 self.textEdit.setText(str(result))          注:如果少了本行代码,那么数据库中的信息将不能显示在text Edit控件中。

3. Combo Box控件,text Edit控件与push Button控件

点击下拉框中的某一信息,点击按钮,进而可以在text Edit控件中显示所要查询的数据信息(前提是已经建立一张数据库信息)
注:此处与2中的不同之处是怎样将按钮信号与Combo Box控件连接起来
代码如下(示例):

yousearch_content = self.comboBox.currentText()        注:很重要
if yousearch_content == "数据信息":
        cursor.execute('select * from awardList')
        result = cursor.fetchone()
elif yousearch_content == "数据信息":
        cursor.execute('select * from awardList limit 1,1')
        result = cursor.fetchone()
elif yousearch_content == "数据信息":
        cursor.execute('select * from awardList limit 2,1')
        result = cursor.fetchone()
...
else:
        result = "无查询信息!"
print(result)
self.plainTextEdit.setPlainText(str(result))
或 self.textEdit.setText(str(result))          注:如果少了本行代码,那么数据库中的信息将不能显示在text Edit控件中。

4. Tree Widget控件(比较综合)

self.treeWidget.itemClicked.connect(self.function)   注:点击item就会弹出相应界面
def function(self):
    if self.treeWidget_2.currentItem().text(0) == "item_1":
        ui_1.show()
    elif self.treeWidget_2.currentItem().text(0) == "item_2":
        ui_2.show()
    elif self.treeWidget_2.currentItem().text(0) == "item_3":
        ui_3.show()

    elif self.treeWidget_2.currentItem().text(0) == "item_4":
        ui_4.show()
    elif self.treeWidget_2.currentItem().text(0) == "item_5":
        ui_5.show()

...

    else:
        ui.show()
注:ui,ui_1...表示各子页面
if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui_1 = first.Ui_Form()
    ui_2 = first_1.Ui_Form()
    ui_3 = first_2.Ui_Form()
    ui_4 = second.Ui_Form()
    ui_5 = second_1.Ui_Form()
...
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

5. TableWidget控件与push Button控件结合(比较综合)

self.pushButton.clicked.connect(self.load)             注:将按钮信号与Table Widget控件连接起来,至关重要
#连接数据库中的表,显示全部信息
def load(self):
    conn = pymysql.Connect(
        host='localhost',                           
        user='root',                                
        password='自己设置的mysql密码',                 
        database='mysql'                            
    )
    cursor = conn.cursor()
    cursor.execute('select * from awardlist')
    data = cursor.fetchall()
    print(data)

    x = 0
    for i in data:
        y = 0
        for j in i:
            self.tableWidget.setItem(x,y,QtWidgets.QTableWidgetItem(str(data[x][y])))
            y = y + 1
        x = x + 1
    cursor.close()
    conn.close()

6. TableWidget控件与push Button控件结合(比较综合)

self.pushButton.clicked.connect(self.load)             注:将按钮信号与Table Widget控件连接起来,至关重要
连接数据库中的表,显示某一行信息
def load(self):
    conn = pymysql.Connect(
        host='localhost',                           
        user='root',                                
        password='自己设置的mysql密码',                 
        database='mysql'                            
    )
    cursor = conn.cursor()
    yousearch_content = self.comboBox.currentText()
    if yousearch_content == "item_1":
        cursor.execute('select * from awardList')
        result = cursor.fetchall()
    elif yousearch_content == "item_2":
        cursor.execute('select * from awardList limit 1,1')
        result = cursor.fetchall()
    elif yousearch_content == "item_3":
        cursor.execute('select * from awardList limit 2,1')
        result = cursor.fetchall()
    elif yousearch_content == "item_4":
        cursor.execute('select * from awardList limit 3,1')
        result = cursor.fetchall()
    elif yousearch_content == "item_5":
        cursor.execute('select * from awardList limit 4,1')
        result = cursor.fetchall()
...
    else:
        result = "无查询信息!"


    print(result)
    print(result[0][0])



显示在表中的第一行
    self.tableWidget.setItem(0,0,QtWidgets.QTableWidgetItem(str(result[0][0])))
    self.tableWidget.setItem(0,1, QtWidgets.QTableWidgetItem(result[0][1]))
    self.tableWidget.setItem(0, 2, QtWidgets.QTableWidgetItem(result[0][2]))


    cursor.close()
    conn.close()

你可能感兴趣的:(qt,ui,开发语言,qt5)