2013-12-18 59 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