简体   繁体   中英

Pyqt - Combobox, how to return selected text

I need help with a program. This is a part from my hole program and i need help with my combobox, i have a combobox ADV, the data of this combo is from my db... I need get the selected item on combobox and pass it as a variable, because after i will create a tablewidget and insert the content of the db using the name as a parameter to take the "things" from my tables... example: i choose "rororo" in this combo.. so its selected.. then pass it as a variable and search about this variable in my db. i know how to do the search, but i dont know how to pass it as a variable.. i have the variable in my "pass_t" def.. but i want to return it to my setupUI def to do my search..

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_())

If I'm not mistaken you just pass it as aa 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM