Results 1 to 8 of 8

Thread: Centering QPushButton within a QTableWidget

  1. #1
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Wink Centering QPushButton within a QTableWidget

    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.
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centering QPushButton within a QTableWidget

    Why don't you just use QListView / QListWidget in icon mode with a grid setup without any push buttons?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    bob2oneil (5th March 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Centering QPushButton within a QTableWidget

    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.
    Attached Images Attached Images

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centering QPushButton within a QTableWidget

    Quote Originally Posted by bob2oneil View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    bob2oneil (6th March 2011)

  7. #5
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: Centering QPushButton within a QTableWidget

    I will defer to your experience and try it.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centering QPushButton within a QTableWidget

    I can give you a hint that you might want to subclass QStyledItemDelegate and reimplement editorEvent() to handle mousePress and mouseRelease events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    bob2oneil (9th March 2011)

  10. #7
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Centering QPushButton within a QTableWidget

    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:
    Qt Code:
    1. //QHBoxLayout *hLayout;
    2. QGridLayout *hLayout;
    3.  
    4. for(int i = 0; i < 128; i++) {
    5.  
    6. but1 = new QPushButton("Read");
    7. but1->setMaximumSize(100, 16777215);
    8. connect(but1, SIGNAL(clicked()), this, SLOT(onSRegRead()));
    9. hLayout = new QGridLayout();//QHBoxLayout();
    10. hLayout->addWidget(but1);
    11. SRegTable->setCellWidget(i, SREG_COL_READ, (QWidget *) hLayout);
    12. }
    To copy to clipboard, switch view to plain text mode 

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Centering QPushButton within a QTableWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Using QPushButton in QTableWidget
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 1st September 2010, 01:26
  2. Can QPushButton be put into a QTableWidget cell?
    By ShaChris23 in forum Newbie
    Replies: 4
    Last Post: 1st September 2010, 01:18
  3. Centering a QCheckBox in a TableView
    By scarleton in forum Qt Programming
    Replies: 2
    Last Post: 6th July 2010, 16:13
  4. How to centering the checkbox in qtableview
    By nemesisqp in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 18:22
  5. Centering a window
    By dwarnold45 in forum Newbie
    Replies: 1
    Last Post: 30th March 2010, 08:43

Tags for this Thread

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.