Hassan Syed
Hassan Syed

Reputation: 20485

QT4 (PyQt4) QTableWidget signal not firing when it should

Program

I am writing an interface to a database of forum posts.

In the program above, There is a toolbox widget housing two widgets with the topic browser active. The topic browser consists of a Parent QFrame with a vertical layout housing the topic forum category label, the navigation control widget and body of the widget which is a QTableWidget (QTW). The QTW selection behaviour is QAbstractItemView.SelectRows.

The problem is that when I connect my function to the itemClicked() signal the signal only fires when I select a cell into the posts and Views columns, but it doesn't fire when I click on the topic name cells. I have tried other signal and they have the same behavior. One thing to note is that the first character of the name is slightly obscured. So i'm thinking maybe this is causing the problem.

Any Advice ?

edit

Code that sets up the table

   class ForumTopicWidget(QFrame):
    def __init__(self,db):
        QFrame.__init__(self)

        self._db=db
        self._rowcount = 21
        self._current_page = 1
        self._forum_id = None
        self._category_id = None

        self._navigation_widget = NavigationHeadWidget()
        self._layout = QVBoxLayout()
        self._title_label = QLabel()
        self._title_label.setText("<b>Topics</b>")
        self._title_label.setAlignment(Qt.AlignCenter)
        self._layout.addWidget(self._title_label)
        self._layout.addWidget(self._navigation_widget)
        self._topic_list = QTableWidget()
        #self._topic_list.setMinimumWidth(600)
        self._topic_list.setRowCount(self._rowcount)
        self._topic_list.setColumnCount(3)
        self._topic_list.setHorizontalHeaderLabels(["Name","Posts", "Views"])
        self._topic_list.horizontalHeader().setResizeMode(0,QHeaderView.Stretch)
        self._topic_list.verticalHeader().setVisible(False)
        #self._topic_list.setColumnWidth(0,476)
        self._topic_list.setColumnWidth(1,61)
        self._topic_list.setColumnWidth(2,61)
        self._topic_list.setSelectionBehavior(QAbstractItemView.SelectRows)
        self._layout.addWidget(self._topic_list)
        self.setLayout(self._layout)
        self.setFrameStyle(QFrame.WinPanel)
        self._topic_list.cellClicked.connect(self.cell_clicked)
        self._navigation_widget.set_nav_functions(self._shift_first, 
                                                  self._shift_prev, 
                                                  self._shift_next, 
                                                  self._shift_last)
        self.page_ids = []

    def cell_clicked(self,item):
        self._prime_topic(self.page_ids[item])

    def set_item_click_function(self,function):
        self._prime_topic = function

    def set_initial_state(self, category_id, forum_name):
        self._category_id = category_id
        self._topic_count = self._db.get_topic_count(category_id)
        self._last_page = int(ceil(float(self._topic_count) / self._rowcount))
        self.shift_state(1) 
        self._title_label.setText("<b> %s Topics</b>" % forum_name)

    def shift_state(self, new_page):
        self._current_page = new_page
        self.populate_page(new_page)
        self._navigation_widget.set_pagination_string(self._current_page, 
                                                      self._last_page)
    def _shift_first(self):
        self.shift_state(1)
        self._current_page = 1

    def _shift_prev(self):
        if self._current_page != 1:
            self._current_page = self._current_page - 1
            self.shift_state(self._current_page)

    def _shift_next(self):
        if self._current_page != self._last_page:
            self._current_page = self._current_page + 1
            self.shift_state(self._current_page)

    def _shift_last(self):
        self.shift_state(self._last_page)

    def populate_page(self,page_number):
        pages = self._db.get_paginated_topics(self._category_id,page_number,self._rowcount);

        i=0
        self.page_ids[:]=[]
        for page in pages:
            self.page_ids.append(page.id)
            q = QLabel()
            q.setText('<font size ="3">%s</font>' % page.title)
            self._topic_list.setCellWidget(i,0, q)
            pc = QTableWidgetItem(str(page.post_count))
            vc = QTableWidgetItem(str(page.view_count))
            pc.setFlags(QtCore.Qt.ItemIsEnabled)
            #pc.setFlags(QtCore.Qt.ItemIsEnabled)
            self._topic_list.setItem(i,1, pc)
            self._topic_list.setItem(i,2, vc)
            i = i +1

The problem shows up on OS X and Linux.

Upvotes: 1

Views: 663

Answers (1)

Hassan Syed
Hassan Syed

Reputation: 20485

Sqwishy from irc channel #qt on freenode pointed out my error.

The first column houses QLabel's and not standard text. QLabels have their own signals. The second 2 columns were types for which the table manages the signals. The reason I used a Label instead of a QTableWidgetItem was because I wanted to resize the font and was under the impression that this was the only way to do it.

The populate_page method should look like this :

   def populate_page(self,page_number):
        pages =\ 
        self._db.get_paginated_topics(self._category_id,page_number,self._rowcount);

        i=0
        self.page_ids[:]=[]
        for page in pages:
            self.page_ids.append(page.id)
            nc = QTableWidgetItem(str(page.title))
            pc = QTableWidgetItem(str(page.post_count))
            vc = QTableWidgetItem(str(page.view_count))
            self._topic_list.setItem(i,0,nc)
            self._topic_list.setItem(i,1, pc)
            self._topic_list.setItem(i,2, vc)
            i = i +1

Upvotes: 1

Related Questions