Results 1 to 7 of 7

Thread: Custom itemClicked() for QTableWidget

  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,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 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,
    _

  3. #3
    Join Date
    Mar 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom itemClicked() for QTableWidget

    I'm sorry. I'm a little confused as to how the connect works and how I'd put that into my code. Do I need to override anything?
    What exactly do I need to include to modify the click behavior?

    I was looking here: https://code.woboq.org/qt5/qtbase/sr...ewidget.h.html and https://code.woboq.org/qt5/qtbase/sr...idget.cpp.html

    To try and figure out how connect and the slot is used within the actual class but I can't figure it out.


    Added after 1 14 minutes:


    I think I do something like this:
    Qt Code:
    1. Table::Table(QWidget *parent) {
    2. connect(ui->board, SIGNAL(itemClicked(QTableWidgetItem*)),
    3. this, SLOT(onItemClicked(QTableWidgetItem*)));
    4. }
    To copy to clipboard, switch view to plain text mode 

    but I am having an error on ui (Use of undeclared identifier ui) and I'm not sure how to fix it.
    Or if I am even doing this much correctly.
    Last edited by Malorn44; 11th March 2019 at 19:31.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,976
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Custom itemClicked() for QTableWidget

    I am guessing that your variable "ui" is a private member of the MainWindow class, and that "ui->board" points to an instance of your Table widget. It is inaccessible from inside your Table class (or any other class besides MainWindow, for that matter).

    I also guess that you don't actually need your Table class at all. You do not need to derive from QTableWidget in order to use signals generated by QTableWidget. If what you want to do is handle an itemClicked() signal generated by QTableWidget, then you should implement that as a slot in your MainWindow class and make the connection in the MainWindow constructor after you have called setupUi():

    Qt Code:
    1. // MainWindow.h
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT;
    6.  
    7. MainWindow( QWidget * parent );
    8.  
    9. // ...
    10.  
    11. private slots:
    12. void onItemClicked( QTableWidgetItem * pItem );
    13.  
    14. // ...
    15. };
    16.  
    17. // MainWindow.cpp
    18.  
    19. MainWindow::MainWindow( QWidget * parent )
    20. : QMainWindow( parent )
    21. // ...
    22. {
    23.  
    24. // Do setup, then this (which uses the new, type-safe version of connect())
    25.  
    26. connect( ui->board, &QTableWidget::itemClicked, this, &MainWindow::onItemClicked );
    27. }
    28.  
    29. void MainWindow::onItemClicked( QTableWidgetItem * pItem )
    30. {
    31. // Do whatever you want with the item (of course, after you have checked to see that pItem is not null)
    32. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom itemClicked() for QTableWidget

    Alright, I got that working. The next thing I need to do is to do the same thing as click when I drag my mouse over cells. I thought I might be able to accomplish this using itemPressed and itemEntered in unison but I'm not sure how to do this without making a boolean pressed to check if both conditions are satisfied. However, there isn't a release signal or anything. Can I do a normal mouse pressed then?

    Edit: nevermind actually. Apparently itemEntered connected to the same slot was enough. I now have to connect the front-end to a backend matrix representation but that shouldn't be too hard.

  6. #6
    Join Date
    Mar 2019
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom itemClicked() for QTableWidget

    I ran into an issue where, when scrolling with a trackpad on an overflowed table, it registers item enters I believe. How do I avoid this? Is there a better way to implement this "drawing" feature? Or can I have drawing do nothing while scrolling?

    Edit: let me know if you think of a better way of handling this. But I decided to just keep track of if the mouse was pressed using the MouseButtonPress event. This seems to have fixed my issue.
    Last edited by Malorn44; 13th March 2019 at 15:06.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,976
    Thanks
    232
    Thanked 627 Times in 617 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Custom itemClicked() for QTableWidget

    Probably what is happening is that when you are scrolling, your finger slips off the scrollbar "thumb" and onto the table, so it registers that as an itemEntered event, even though the scrollbar still has the input focus because the button is still pressed. Same thing that happens when you press the mouse on a button and then move off it while the button is still pressed. The button highlight will change, but if you move back onto the button to release the mouse, it will cause a click event.

    Keeping track of the state of the mouse buttons is probably the best way to handle your situation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 31st December 2018, 12:47
  2. No such signal QListWidget::itemClicked
    By ouekah in forum Newbie
    Replies: 1
    Last Post: 22nd February 2010, 14:48
  3. ListWidget itemClicked problem
    By Keemosabi in forum Newbie
    Replies: 2
    Last Post: 11th August 2008, 22:29
  4. itemDoubleClicked and itemClicked
    By miraks in forum Qt Programming
    Replies: 8
    Last Post: 8th August 2008, 16:52
  5. QTreeWidget fails to emit itemClicked signal.
    By avh in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2008, 18: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.