Results 1 to 13 of 13

Thread: Deselect lines in table views

  1. #1
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Deselect lines in table views

    Hi,

    I would like to implement the following behavior on my QTableView:
    left clicking in a blank area should act as "deselect" (any selected line should be unselected).

    Do you know how to implement this ?

    Thank you in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Deselect lines in table views

    Reimplement mousePressEvent(), check event button & itemAt(), and if it returns null, call clearSelection().
    J-P Nurmi

  3. #3
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile [resolved] Deselect lines in table views

    Quote Originally Posted by jpn View Post
    Reimplement mousePressEvent(), check event button & itemAt(), and if it returns null, call clearSelection().
    Hi jpn,

    Thank you for your help, I did it like this:
    Qt Code:
    1. void SKGTableView::mousePressEvent ( QMouseEvent * event )
    2. {
    3. if (event->button()==Qt::LeftButton && !(this->indexAt(event->pos()).isValid())) {
    4. clearSelection();
    5. }
    6. QTableView::mousePressEvent(event);
    7. }
    To copy to clipboard, switch view to plain text mode 

    It works as expected !

  4. #4
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [resolved] Deselect lines in table views

    To reimplement (for example) mousePressEvent I have to subclass QTableWidget/View at first, right? And I won't be able to position it via Qt Designer?

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [resolved] Deselect lines in table views

    Quote Originally Posted by kremuwa View Post
    To reimplement (for example) mousePressEvent I have to subclass QTableWidget/View at first, right?
    Right.
    And I won't be able to position it via Qt Designer?
    Sure you are. Use the promote option in designer. Or of course write a custom plugin...

  6. #6
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [resolved] Deselect lines in table views

    Could you expand a little bit this "promote" option? All of the widgets in my app are created using Qt Designer and I don't want to change it now.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [resolved] Deselect lines in table views

    right click on any widget in your designer and then you have the option (above goto slot) promote to. There you can say, that your own class should be used.

  8. #8
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [resolved] Deselect lines in table views

    I have prepared such a class:
    Qt Code:
    1. class upgTableWidget : public QTableWidget
    2. {
    3. void mouseReleaseEvent(QMouseEvent *event)
    4. {
    5. if(event->button() == Qt::LeftButton && !(this->indexAt(event->pos()).isValid()))
    6. this->clearSelection();
    7. QTableWidget::mouseReleaseEvent(event);
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    ...in a separate *.h file. I used that "Promote to..." option and I got such errors, coming from "ui_mainwindow.h" file (mainwindow is the class of the window where this table is placed):
    C:/Users/K1/Desktop/Dengo - Projekt/mainwindow.h:4: In file included from mainwindow.h:4,
    C:/Users/K1/Desktop/Dengo - Projekt/mainwindow.cpp:1: from mainwindow.cpp:1:
    C:/Users/K1/Desktop/Dengo - Projekt/ui_mainwindow.h:174: error: no matching function for call to 'upgTableWidget::upgTableWidget(QWidget*&)'
    C:/Users/K1/Desktop/Dengo - Projekt/./upgtablewidget.h:8: note: candidates are: upgTableWidget::upgTableWidget()
    C:/Users/K1/Desktop/Dengo - Projekt/./upgtablewidget.h:8: note: upgTableWidget::upgTableWidget(const upgTableWidget&)
    I tried to change the constructor call to the one without any arguments and it worked, but I think that if I change something in the GUI I'll have to regenerate the ui_ file and the error will show up again. What do you think about it, maybe I should ensure additional constructor in my class?

    I've got another problem, too. As I said before, the tables (yes, there are two tables and I want both of them to be of type upgTableWidget) are placed on the window of class MainWindow which is a subclass of QMainWindow. When I click on one of the tables, if the second one was selected, I want to clearSelection in the second table. My problem is, that I don't know how to access the second table - it should be probably made with some kind of parent and it may be something related to the first problem.

    Thanks in advance for any advices,
    Michał
    Last edited by kremuwa; 14th August 2010 at 07:24.

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [resolved] Deselect lines in table views

    Simple provide a standard constructor taking a QWidget as parent and pass it to the QTableWidget base class.
    Qt Code:
    1. upgTableWidget(QWidget *parent = 0) : QTableWidget(parent) {}
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [resolved] Deselect lines in table views

    Ok, it works... But what exactly is passed do the parent object? How can I access the second table placed on the same window to clear it's selection?

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [resolved] Deselect lines in table views

    Quote Originally Posted by kremuwa View Post
    Ok, it works... But what exactly is passed do the parent object?
    the parent.
    How can I access the second table placed on the same window to clear it's selection?
    What second table?

  12. #12
    Join Date
    Feb 2010
    Posts
    68
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [resolved] Deselect lines in table views

    I've got mainwindow.ui file which contains QMainWindow object with two tables of type upgTableWidget placed on it. Currently, when there is a selection in the first table and I click an item of the second one, I have two selections at the same time (the first one loses focus but it's still selected). What I want to do is to have only one selection at the same time. So I thought, that in the reimplemented mouseReleaseEvent() function I'll clear the selection of the table previously selected. But to do that, I have to access it somehow. The question is, how to do that.

    You must be really irritated because of my silliness, but hold on a little longer.

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [resolved] Deselect lines in table views

    Use signal and slots. Try something like that:
    Qt Code:
    1. w1.setRowCount(5);
    2. w1.setColumnCount(3);
    3.  
    4. w2.setRowCount(5);
    5. w2.setColumnCount(3);
    6.  
    7. for(int row = 0; row < 5; ++row)
    8. {
    9. for(int column = 0; column < 3; ++column)
    10. {
    11. QTableWidgetItem *newItem = new QTableWidgetItem(QString("%1").arg((row+1)*(column+1)));
    12. w1.setItem(row, column, newItem);
    13. }
    14. }
    15.  
    16. for(int row = 0; row < 5; ++row)
    17. {
    18. for(int column = 0; column < 3; ++column)
    19. {
    20. QTableWidgetItem *newItem = new QTableWidgetItem(QString("%1").arg((row+1)*(column+1)));
    21. w2.setItem(row, column, newItem);
    22. }
    23. }
    24.  
    25. l.addWidget(&w1);
    26. l.addWidget(&w2);
    27. w.setLayout(&l);
    28. w.show();
    29.  
    30. QObject::connect(&w1, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)), &w2, SLOT(clearSelection()));
    To copy to clipboard, switch view to plain text mode 
    But be aware that this is just a demonstration. You have to make a own slot, from where you clear the table widgets and make sure that you don't get a infinite loop. To know which table widget calls the slot use QTableWidgetItem::tableWidget().

  14. The following user says thank you to Lykurg for this useful post:

    kremuwa (15th August 2010)

Similar Threads

  1. How to add new lines in a table view
    By klaus1111 in forum Newbie
    Replies: 1
    Last Post: 17th August 2006, 11:45

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.