开发者

List view is not refreshed if setTabText() is called

开发者 https://www.devze.com 2022-12-12 23:32 出处:网络
Yes, I know this sounds crazy. But here\'s the situation. I composed a minimal code reproducing the bug. The code creates main window with QTabWidget, which, in turn, has one tab with QListView and a

Yes, I know this sounds crazy. But here's the situation.

I composed a minimal code reproducing the bug. The code creates main window with QTabWidget, which, in turn, has one tab with QListView and a button. List view is connected to QAbstractListModel. Initially, list model contains empty list. If user clicks on a button, it is populated with 3 elements and corresponding signal is emitted. On this signal, tab widget emits a signal with new title, which is caught by QMainWindow and used to change tab title.

So, the problem is, if I call setTabText() with this new title, list view remains empty until I click on it (then new items instantly appear). If I use new title in setWindowTitle() instead, new items appear in list view right after pressing the button. Am I doing something wrong, or is there some bug in QTabWidget (or Python mapping)?

Code is the following:

from PyQt4 import QtGui, QtCore
import sys


class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.setWindowTitle("Test")
        self._tabbar = QtGui.QTabWidget()
        self.setCentralWidget(self._tabbar)

        tab = SearchWindow(self)
        tab.titleChanged.connect(self._refreshTabTitle)
        self._tabbar.addTab(tab, "Initial title")

    def _refreshTabTitle(self, title):
        # if line 1 is commented - no bug, if line 2 is commented - bug exists
        self._tabbar.setTabText(0, title) # line 1
        #self.setWindowTitle(title) # line 2


class SearchWindow(QtGui.QSplitter):

    titleChanged = QtCore.pyqtSignal(str)

    def __init__(self, parent):
        QtGui.QSplitter.__init__(self, QtCore.Qt.Vertical, parent)

        results开发者_如何学Go_model = ResultsModel(self)

        results_view = QtGui.QListView()
        results_view.setModel(results_model)
        self.addWidget(results_view)

        search_button = QtGui.QPushButton(">>")
        search_button.clicked.connect(results_model.refreshResults)
        self.addWidget(search_button)

        results_model.searchFinished.connect(self._refreshTitle)

    def _refreshTitle(self):
        self.titleChanged.emit("New title")


class ResultsModel(QtCore.QAbstractListModel):

    searchFinished = QtCore.pyqtSignal()

    def __init__(self, parent):
        QtCore.QAbstractListModel.__init__(self, parent)
        self._results = []

    def rowCount(self, parent):
        return len(self._results)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if not index.isValid():
            return None
        elif index.row() = len(self._results):
            return None
        elif role == QtCore.Qt.DisplayRole:
            return self._results[index.row()]

    def refreshResults(self):
        self._results = ['result1', 'result2', 'result3']
        self.reset()
        self.searchFinished.emit()


app = QtGui.QApplication(sys.argv)
wnd = MainWindow()
wnd.show()
sys.exit(app.exec_())

Tested on Mac OS 10.6.2, Qt SDK 2009.04 (4.5), pyQt 4.6.1 (maybe this is the problem and I need to use 4.5?), Python 3.1.


Could not reproduce your problem using Linux, Qt 4.5.3, pyQt 4.5.4, python 2.5.2.

I guess this is definitely version/platform-dependent. You should try Qt 4.5.3 + pyQt 4.5.4 + python 2.5.2 on MacOS. If you can reproduce the problem, it is more like a bug in MacOS qt port. If you can't you should try newer qt versions under Windows or Linux.

0

精彩评论

暂无评论...
验证码 换一张
取 消