LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2015, 12:19 AM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
pyqt4 qtablewidget / select and/or highlight other cells when one is clicked.


Using python 2.7.6 and QT 4.8.6
Mint Linux 17.1

Using qtablewidget with 16 rows and 4 colums. Need to automatically select other specific cells in the table when one of the cells is selected.

The row of the selection needs to be selected plus 6 more cells in the column.

Have not found any code examples that select a cell. All examples I have found assume that all selections are made manually.

Any help would be appreciated.

Cheers,
 
Old 02-22-2015, 12:39 PM   #2
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Any suggestions would help. To get me started if someone can explain how to simply select a cell would be great.
 
Old 02-22-2015, 02:33 PM   #3
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
One way to do this is with a delegate. You can highlight items with QItemDelegate.paint().

I cobbled this example together by translating from C++, so TIFWIW:
Code:
#!/usr/bin/python
import sys
from PyQt4.Qt import *

class MyTable(QTableWidget):
    def __init__(self,  *args):
        QTableWidget.__init__(self, *args)
        self.setmydata()
        self.itemSelectionChanged.connect(self.slot)
        self.setItemDelegate(delegate(self))
        
    def slot(self):
        self.viewport().update()
        
    def setmydata(self):
        for row in range(0,16):
            for col in range(0,4):
                newitem = QTableWidgetItem(QString("row %1, col %2").arg(row+1).arg(col+1))
                if row == 0 and col == 0:
                    newitem.setText("click me")
                self.setItem(row, col, newitem)

class delegate(QItemDelegate):                
    def __init__(self,parent):
        super(delegate,self).__init__(parent)
        self.selectionmodel = parent.selectionModel()
        
    def paint(self,painter,option,index):
        if index == index.model().index(12,3) or index == index.model().index(13,1): 
        #if index.row() == 12 and index.column() == 3:
            if self.selectionmodel.isSelected(index.sibling(0,0)):
                painter.fillRect(option.rect, QColor(68, 171, 230))
        painter.drawText(option.rect, Qt.AlignCenter, index.data().toString())

def main(args):
    app = QApplication(args)
    table = MyTable(16, 4)
    table.resize(500,600)
    table.show()
    sys.exit(app.exec_())
    
if __name__=="__main__":
    main(sys.argv)
Hope this helps.
 
Old 02-23-2015, 11:37 AM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
This is helpful. Is there also a way to set the other cells to selected?

Regards,


Quote:
Originally Posted by norobro View Post
One way to do this is with a delegate. You can highlight items with QItemDelegate.paint().

I cobbled this example together by translating from C++, so TIFWIW:
Code:
#!/usr/bin/python
import sys
from PyQt4.Qt import *

class MyTable(QTableWidget):
    def __init__(self,  *args):
        QTableWidget.__init__(self, *args)
        self.setmydata()
        self.itemSelectionChanged.connect(self.slot)
        self.setItemDelegate(delegate(self))
        
    def slot(self):
        self.viewport().update()
        
    def setmydata(self):
        for row in range(0,16):
            for col in range(0,4):
                newitem = QTableWidgetItem(QString("row %1, col %2").arg(row+1).arg(col+1))
                if row == 0 and col == 0:
                    newitem.setText("click me")
                self.setItem(row, col, newitem)

class delegate(QItemDelegate):                
    def __init__(self,parent):
        super(delegate,self).__init__(parent)
        self.selectionmodel = parent.selectionModel()
        
    def paint(self,painter,option,index):
        if index == index.model().index(12,3) or index == index.model().index(13,1): 
        #if index.row() == 12 and index.column() == 3:
            if self.selectionmodel.isSelected(index.sibling(0,0)):
                painter.fillRect(option.rect, QColor(68, 171, 230))
        painter.drawText(option.rect, Qt.AlignCenter, index.data().toString())

def main(args):
    app = QApplication(args)
    table = MyTable(16, 4)
    table.resize(500,600)
    table.show()
    sys.exit(app.exec_())
    
if __name__=="__main__":
    main(sys.argv)
Hope this helps.
 
Old 02-23-2015, 12:24 PM   #5
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by sharky View Post
Is there also a way to set the other cells to selected?
Sure. Make the following change in paint():
Code:
#painter.fillRect(option.rect, QColor(68, 171, 230))
self.selectionmodel.select(index, QItemSelectionModel.Select)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PySide vs PyQt4 problem sharky Programming 5 06-24-2014 01:41 PM
How do I know if I have PyQT4 installed using Ubuntu? ShaqDiesel Ubuntu 1 05-15-2013 01:37 PM
[SOLVED] PyQt4 Make errors asuderma Linux - Software 1 07-10-2012 12:37 AM
PyQt4 app broken MTK358 Programming 5 10-21-2010 10:13 AM
PyQt4 Signals and Slots MTK358 Programming 3 06-20-2010 08:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration