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.
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.
Reimplement mousePressEvent(), check event button & itemAt(), and if it returns null, call clearSelection().
J-P Nurmi
Hi jpn,
Thank you for your help, I did it like this:
Qt Code:
{ if (event->button()==Qt::LeftButton && !(this->indexAt(event->pos()).isValid())) { clearSelection(); } }To copy to clipboard, switch view to plain text mode
It works as expected !
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?
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.
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.
I have prepared such a class:
Qt Code:
{ { if(event->button() == Qt::LeftButton && !(this->indexAt(event->pos()).isValid())) this->clearSelection(); } };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):
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?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'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.
Simple provide a standard constructor taking a QWidget as parent and pass it to the QTableWidget base class.Qt Code:
To copy to clipboard, switch view to plain text mode
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?
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.
Use signal and slots. Try something like that: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().Qt Code:
QTableWidget w1; w1.setRowCount(5); w1.setColumnCount(3); QTableWidget w2; w2.setRowCount(5); w2.setColumnCount(3); for(int row = 0; row < 5; ++row) { for(int column = 0; column < 3; ++column) { w1.setItem(row, column, newItem); } } for(int row = 0; row < 5; ++row) { for(int column = 0; column < 3; ++column) { w2.setItem(row, column, newItem); } } QWidget w; QHBoxLayout l; l.addWidget(&w1); l.addWidget(&w2); w.setLayout(&l); w.show(); QObject::connect(&w1, SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)), &w2, SLOT(clearSelection()));To copy to clipboard, switch view to plain text mode
kremuwa (15th August 2010)
Bookmarks