Results 1 to 7 of 7

Thread: Custom itemClicked() for QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Question Custom itemClicked() for QTableWidget

    I'm trying to create a custom class for QTableWidget where when clicking on a cell (or eventually just pressing the mouse down over a cell) it changes the background color.
    This is what I have so far... it's fairly simple but it doesn't work yet and I'm not sure what I'm missing. Sorry, I'm new to the Qt environment.

    Excerpt from mainwindow.cpp
    Qt Code:
    1. int row = 10;
    2. int col = 10;
    3.  
    4. // set the amount of rows and columns
    5. ui->board->setRowCount(row);
    6. ui->board->setColumnCount(col);
    7.  
    8. // remove table headers
    9. ui->board->horizontalHeader()->setVisible(false);
    10. ui->board->verticalHeader()->setVisible(false);
    11.  
    12. // table styling
    13. ui->board->setFocusPolicy(Qt::NoFocus);
    14. ui->board->setSelectionMode(QAbstractItemView::NoSelection);
    15. // ui->board->setStyleSheet("selection-background-color: transparent");
    16. ui->board->setEditTriggers(QAbstractItemView::NoEditTriggers);
    17.  
    18. // set fixed column and row width
    19. for (int i = 0; i < row; i++)
    20. ui->board->setRowHeight(i, 50);
    21. for (int i = 0; i < col; i++)
    22. ui->board->setColumnWidth(i, 50);
    23.  
    24. // populate with items
    25. for (int i = 0; i < row; i++) {
    26. for (int j = 0; j < col; j++) {
    27. ui->board->setItem(i, j, new QTableWidgetItem);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    excerpt from table.h
    Qt Code:
    1. #include <QTableWidget>
    2. class Table : public QTableWidget
    3. {
    4. public:
    5. Table(QWidget *parent = nullptr) {}
    6.  
    7. Q_SIGNALS:
    8. void itemClicked(QTableWidgetItem *item);
    9.  
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    excerpt from table.cpp
    Qt Code:
    1. #include "table.h"
    2.  
    3. void Table::itemClicked(QTableWidgetItem *item) {
    4. item->setBackground(Qt::red);
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Custom itemClicked() for QTableWidget

    itemClicked() is a signal

    If you want to react to a signal you connect it to a slot.
    See QObject::connect().

    Cheers,
    _

Similar Threads

  1. No such signal QListWidget::itemClicked
    By ouekah in forum Newbie
    Replies: 1
    Last Post: 22nd February 2010, 15:48
  2. ListWidget itemClicked problem
    By Keemosabi in forum Newbie
    Replies: 2
    Last Post: 11th August 2008, 23:29
  3. itemDoubleClicked and itemClicked
    By miraks in forum Qt Programming
    Replies: 8
    Last Post: 8th August 2008, 17:52
  4. QTreeWidget fails to emit itemClicked signal.
    By avh in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2008, 19:49

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.