2013-12-18 64 views
1

我需要在QWidget(其中包含QTableView)按Ctrl+F後顯示查找對話框。查找對話框將在表格的第一列進行搜索以查找匹配項。如何顯示QDialog

我可以用下面的代碼按Ctrl+F後顯示QMessageBox提示:

class Widget(QWidget): 
    def __init__(self,md,parent=None): 
     QWidget.__init__(self,parent) 
     layout=QVBoxLayout(self) 

     # initially construct the visible table 
     tv = QTableView() 
     # uncomment this if the last column shall cover the rest 
     tv.horizontalHeader().setStretchLastSection(True) 
     tv.show() 

     # set black grid lines 
     self.setStyleSheet("gridline-color: rgb(39, 42, 49)") 

     # construct the Qt model belonging to the visible table 
     model = NvmQtModel(md) 
     tv.setModel(model) 
     tv.resizeRowsToContents() 
     tv.resizeColumnsToContents() 

     # set the shortcut ctrl+F for find in menu 
     shortcut = QShortcut(QKeySequence('Ctrl+f'), self) 
     shortcut.activated.connect(self.handleFind) 

     # delegate for decimal 
     delegate = NvmDelegate() 
     tv.setItemDelegate(delegate) 
     self.setGeometry(200,200,600,600) # adjust this later 
     layout.addWidget(tv) 

     # set window title 
     self.setWindowTitle("TITLE") 

     # find function: search in the first column of the table 
     def handleFind(self): 
      reply = QMessageBox.question(
       self, 'Find', 'Find Dialog', 
       QMessageBox.Yes | QMessageBox.No) 
      if reply == QMessageBox.Yes: 
       print('Yes') 
      else: 
       print('No') 

然後,我改變了QMessageBoxQDialog,但現在不工作。我將不勝感激,如果你能告訴我,我沒有做正確:

class Widget(QWidget): 
    def __init__(self,md,parent=None): 
     QWidget.__init__(self,parent) 
     layout=QVBoxLayout(self) 

     # initially construct the visible table 
     tv = QTableView() 
     # uncomment this if the last column shall cover the rest 
     tv.horizontalHeader().setStretchLastSection(True) 
     tv.show() 

     # set black grid lines 
     self.setStyleSheet("gridline-color: rgb(39, 42, 49)") 

     # construct the Qt model belonging to the visible table 
     model = NvmQtModel(md) 
     tv.setModel(model) 
     tv.resizeRowsToContents() 
     tv.resizeColumnsToContents() 

     # set the shortcut ctrl+F for find in menu 
     shortcut = QShortcut(QKeySequence('Ctrl+f'), self) 
     shortcut.activated.connect(self.handleFind) 

     # delegate for decimal 
     delegate = NvmDelegate() 
     tv.setItemDelegate(delegate) 
     self.setGeometry(200,200,600,600) # adjust this later 
     layout.addWidget(tv) 

     # set window title 
     self.setWindowTitle("TITLE") 

    # find function: search in the first column of the table 
    def handleFind(self): 
     findDialog = QDialog() 
     findLabel = QLabel("Find what", findDialog) 
     findField = QLineEdit(findDialog) 
     findButton = QPushButton("Find", findDialog) 
     closeButton = QPushButton("Close", findDialog) 
     findDialog.show() 

回答

2

如果你想在對話框中一個模式對話框,叫findDialog.exec_()

from PyQt4.QtGui import * 

def handleFind(): 
    findDialog = QDialog() 
    #findDialog.setModal(True) 
    findLabel = QLabel("Find what", findDialog) 
    findField = QLineEdit(findDialog) 
    findButton = QPushButton("Find", findDialog) 
    closeButton = QPushButton("Close", findDialog) 
    #findDialog.show() 
    findDialog.exec_() 


app = QApplication([]) 

b = QPushButton("click me")  
b.clicked.connect(handleFind) 
b.show() 

app.exec_() 
+0

由於現在alot..it顯示!但如何調整一切的位置?他們看起來不太好..也發現按鈕被關閉按鈕覆蓋! –

+1

您應該使用佈局對象來佈置小部件。 – HYRY