2013-12-18 3 views
1

Мне нужно показать диалог поиска после нажатия Ctrl+F в QWidget, который содержит QTableView. Диалоговое окно поиска будет искать в первом столбце таблицы, чтобы найти совпадения.Как показать QDialog

Я могу показать QMessageBox после нажатия Ctrl+F со следующим кодом:

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') 

Тогда я изменил QMessageBox к QDialog, но сейчас она не работает. Я был бы признателен, если вы могли бы сказать мне, где я не делаю это правильно:

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

Смежные вопросы