PDA

View Full Version : Deselect lines in table views



miraks
12th January 2009, 15:15
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.

jpn
12th January 2009, 18:07
Reimplement mousePressEvent(), check event button & itemAt(), and if it returns null, call clearSelection().

miraks
14th January 2009, 21:27
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:

void SKGTableView::mousePressEvent ( QMouseEvent * event )
{
if (event->button()==Qt::LeftButton && !(this->indexAt(event->pos()).isValid())) {
clearSelection();
}
QTableView::mousePressEvent(event);
}

It works as expected !:)

kremuwa
12th August 2010, 12:23
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?

Lykurg
12th August 2010, 13:04
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...

kremuwa
12th August 2010, 13:31
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.

Lykurg
12th August 2010, 14:07
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.

kremuwa
14th August 2010, 07:13
I have prepared such a class:

class upgTableWidget : public QTableWidget
{
void mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton && !(this->indexAt(event->pos()).isValid()))
this->clearSelection();
QTableWidget::mouseReleaseEvent(event);
}
};

...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ł

Lykurg
14th August 2010, 07:19
Simple provide a standard constructor taking a QWidget as parent and pass it to the QTableWidget base class.
upgTableWidget(QWidget *parent = 0) : QTableWidget(parent) {}

kremuwa
14th August 2010, 15:07
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?

Lykurg
14th August 2010, 15:38
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?

kremuwa
14th August 2010, 16:10
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. :eek:

Lykurg
14th August 2010, 23:16
Use signal and slots. Try something like that:
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)
{
QTableWidgetItem *newItem = new QTableWidgetItem(QString("%1").arg((row+1)*(column+1)));
w1.setItem(row, column, newItem);
}
}

for(int row = 0; row < 5; ++row)
{
for(int column = 0; column < 3; ++column)
{
QTableWidgetItem *newItem = new QTableWidgetItem(QString("%1").arg((row+1)*(column+1)));
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*,QTable WidgetItem*)), &w2, SLOT(clearSelection()));
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().