0

我需要有关程序的帮助。这是我的洞程序的一部分,我需要组合框的帮助,我有一个组合框 ADV,这个组合的数据来自我的数据库...我需要在组合框上获取所选项目并将其作为变量传递,因为在我将创建一个表格小部件并使用名称作为参数插入数据库的内容以从我的表中获取“事物”之后......例如:我在这个组合中选择“rororo”......所以它被选中......然后通过它作为一个变量并在我的数据库中搜索这个变量。我知道如何进行搜索,但我不知道如何将它作为变量传递..我的“pass_t”定义中有变量..但我想将它返回到我的 setupUI def 进行搜索..

from PyQt4.QtGui import * 
from PyQt4.QtSql import * 
from PyQt4.QtCore import * 
import sys

try:
  _fromUtf8 = QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_MainWindow(object):

  def setupUi(self, MainWindow):
      MainWindow.setObjectName(_fromUtf8("MainWindow"))
      MainWindow.resize(800, 600)
      self.centralwidget = QWidget(MainWindow)
      self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
      self.comboADV = QComboBox(self.centralwidget)
      self.comboADV.setGeometry(QRect(110, 80, 91, 31))
      self.comboADV.setObjectName(_fromUtf8("comboADV"))
      self.comboADV.addItem(_fromUtf8(""))
      db = QSqlDatabase.addDatabase("QMYSQL")
      db.setHostName('localhost')
      db.setDatabaseName('database')
      db.setUserName('username')
      db.setPassword('password')
      if (db.open()==False):     

        QMessageBox.critical(None, "Database Error",
            db.lastError().text())   
      query  = QSqlQuery ("SELECT Palavra FROM Tabela053 WHERE Categoria='Adv.' ORDER BY Palavra ASC;")
      index=0
      while (query.next()):
      self.comboADV.addItem(query.value(0).toString())
      index = index+1
      self.comboADV.activated.connect(self.passtt) #want to change this
#---------------------------------------important
  def passtt(self):
      a = self.comboADV.currentText()
      print a #here i can print the selected content of my comboADV
if __name__ == "__main__":
  import sys

  app = QApplication(sys.argv)
  MainWindow = QMainWindow()

  ui = Ui_MainWindow()
  ui.setupUi(MainWindow)

MainWindow.show()
sys.exit(app.exec_())
4

1 回答 1

1

If I'm not mistaken you just pass it as a a parameter in your passtt function. But first you need to change your signal:

self.comboADV.activated.connect(self.passtt) -> self.comboADV.activated[str].connect(self.passtt)

Then:

def passtt(self,item):
    print(item)
于 2013-08-15T02:41:16.967 回答