PDA

View Full Version : Centering QPushButton within a QTableWidget



bob2oneil
4th March 2011, 23:51
Does anyone know how to center a QPushButton within a QTableWidget.
I need to create a dynamic set of menu icons in a grid, with scrollbars for
content that may be off screen. The QTableWidget seems to work to some
extent, but interested if others think a better Qt list class would be better.

Attached is a sample screenshot. I want the pushbuttons to be centered
within the larger grid size of the table.

I have left the grid lines on for reference, but these will be removed in practice.

Do the text alignment flags work here, or is a delegate required, or perhaps a derivative
of QTableWidget.

wysota
5th March 2011, 01:33
Why don't you just use QListView / QListWidget in icon mode with a grid setup without any push buttons?

bob2oneil
5th March 2011, 19:53
Hi wysota, thanks for helping out again. I need to have the pushbutton behavior emulating the modern cellphone, where I have defined a bitmap to use when the button gets clicked upon (see screenshot).

I need to have the list view arranged with 4 icon width, and variable length height that is scrollable vertically. One solution I pondered was to create a custom widget including the pushbutton with spacers on all sides, and add this item to the table view to get it centered. All of the custom styling and event filtering would live in this widget alone.

If it is your belief that a QListView or QListWidget is sufficient for these requirements, I will give it a try. I know that when using a QListWidget for a multiline list control with kinetic scrolling, I had to implement getHintSize() and other support functions, and it felt a bit like trying to replace a standard list item with custom content was a bit of a hack.

I wonder how this approach might compare with simply creating a custom widget and using the QTableView.

I should mention that the number of icons used in the grid pattern will be dynamic and runtime changeable, in both text and icon displayed, and perhaps will exceed the size of the visible screen in terms of count (20 maximum)-- hence the need for a parent control with vertical scroll support.

wysota
5th March 2011, 20:15
Hi wysota, thanks for helping out again. I need to have the pushbutton behavior emulating the modern cellphone, where I have defined a bitmap to use when the button gets clicked upon (see screenshot).
There are no push buttons in "modern cellphones". And you don't need any, a plain old QListView will suffice, optionally with a custom delegate to do some advanced stuff (like changing the icon upon click).


I need to have the list view arranged with 4 icon width, and variable length height that is scrollable vertically.
Still sounds like QListView/QListWidget.


I know that when using a QListWidget for a multiline list control with kinetic scrolling, I had to implement getHintSize() and other support functions, and it felt a bit like trying to replace a standard list item with custom content was a bit of a hack.
I have no idea what you are talking about.


I wonder how this approach might compare with simply creating a custom widget and using the QTableView.
Again, using widgets with ItemViews doesn't make sense.


I should mention that the number of icons used in the grid pattern will be dynamic and runtime changeable, in both text and icon displayed, and perhaps will exceed the size of the visible screen in terms of count (20 maximum)-- hence the need for a parent control with vertical scroll support.
Still sounds like QListView/QListWidget.

Of course you can also use graphics view for all this. But certainly no push buttons.

bob2oneil
6th March 2011, 19:57
I will defer to your experience and try it.

wysota
6th March 2011, 20:47
I can give you a hint that you might want to subclass QStyledItemDelegate and reimplement editorEvent() to handle mousePress and mouseRelease events.

grantbj74
11th November 2011, 07:44
Is there a way to center buttons in a table?

I tried to add a layout with button inside to the table. It compiled but died during run.

Failed code:


QPushButton *but1;
//QHBoxLayout *hLayout;
QGridLayout *hLayout;

for(int i = 0; i < 128; i++) {

but1 = new QPushButton("Read");
but1->setMaximumSize(100, 16777215);
connect(but1, SIGNAL(clicked()), this, SLOT(onSRegRead()));
hLayout = new QGridLayout();//QHBoxLayout();
hLayout->addWidget(but1);
SRegTable->setCellWidget(i, SREG_COL_READ, (QWidget *) hLayout);
}

wysota
11th November 2011, 11:26
Casting a layout to a widget is similar to saying "let's pretend this apple is a key" and trying to open a door with it.