PDA

View Full Version : How to prevent QTableWidget from inserting a new row



lain
27th March 2012, 01:10
QTableWidget inserts a new row when Enter key is pressed in a blank cell.
How can I suppress this behavior?
Should I catch the enter-key event using an event filter?

Spitfire
28th March 2012, 13:20
Hmmm does it?
That's interesting.

You're sure you're not doing it somehow?

Which Qt version you're using?

lain
29th March 2012, 01:23
Hi, thank you for reply.

I am using Qt 4.8.0 on Linux/X11.
When I select a cell with a mouse click and press the enter key, a new row is inserted below the selected cell.
Doesn't it be like this in your environment?

ChrisW67
29th March 2012, 01:46
Linux, Qt 4.7.4 or Qt 4.8.0, this code

#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTableWidget tw(5, 5);
tw.show();
return app.exec();
}

Behaves as expected.

Do you have an active push button elsewhere in the UI that adds a row?

lain
29th March 2012, 02:38
Thank you for reply.

Do you have an active push button elsewhere in the UI that adds a row?
You've hit the nail on the head.
I have several buttons on the table widget, and one of them right below the QTableWidget is connected to a slot that inserts a row.
I found that the button is somehow also selected every time I clicked a cell in the table and the slot is called when the enter key is pressed after that.
I don't know how this happens...

ChrisW67
29th March 2012, 03:41
It will be related to the presence of default and auto-default buttons on the dialog. If you press Enter when not editing a cell of your table then one of the buttons will be triggered. When editing a cell the table should swallow the Enter keypress.

lain
29th March 2012, 05:46
Thanks for telling me about "default" and "autoDefaut" properties of a button.
This may not be the best solution, but I worked around by turning off autoDefault property of all the buttons.