2016-09-27 19 views
1

編集不可能なQComboBoxのテキストの配置を変更することはできますか?この答えは、中心編集不可QComboBoxテキスト(PyQt付き)

How to center text in QComboBox?

QComboBoxを編集可能にしかし、私はその変化に、コンボボックスのクリック可能な部分がリストをトリガするので、右に今だけ矢印であることを望んでいないことによってこれを実現します一方、領域全体がクリック可能になる前に、ドロップダウンメニューがトリガされます。

答えて

1

答えは部分的に既にリンクされた質問に記載されていますHow to center text in QComboBox? 残っているのは、読み取り専用オブジェクトをクリック可能にすることです。これは、hereのように行うことができます。 ...次のコードを入力してください:

from PyQt4 import QtGui, QtCore 

class Window(QtGui.QWidget): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 
     layout = QtGui.QVBoxLayout(self) 
     self.combo = QtGui.QComboBox() 
     self.combo.setEditable(True) 
     self.ledit = self.combo.lineEdit() 
     self.ledit.setAlignment(QtCore.Qt.AlignCenter) 
     # as suggested in the comment to 
     # https://stackoverflow.com/questions/23770287/how-to-center-text-in-qcombobox 
     self.ledit.setReadOnly(True) # 
     self.combo.addItems('One Two Three Four Five'.split()) 
     layout.addWidget(self.combo) 
     self.clickable(self.combo).connect(self.combo.showPopup) 
     self.clickable(self.ledit).connect(self.combo.showPopup) 

    def clickable(self,widget): 
     """ class taken from 
     https://wiki.python.org/moin/PyQt/Making%20non-clickable%20widgets%20clickable """ 
     class Filter(QtCore.QObject): 
      clicked = QtCore.pyqtSignal() 
      def eventFilter(self, obj, event): 
       if obj == widget: 
        if event.type() == QtCore.QEvent.MouseButtonRelease: 
         if obj.rect().contains(event.pos()): 
          self.clicked.emit() 
          # The developer can opt for .emit(obj) to get the object within the slot. 
          return True 
       return False 
     filter = Filter(widget) 
     widget.installEventFilter(filter) 
     return filter.clicked 

if __name__ == '__main__': 

    import sys 
    app = QtGui.QApplication(sys.argv) 
    window = Window() 
    window.show() 
    sys.exit(app.exec_()) 
+0

はい、望ましい結果が得られます。より良い方法が必要であるように思えます。レスポンスありがとう –

関連する問題