×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beijing, China
    Posts
    386

    QTableWidget:how to control size?

    Hi,

    I have a layout like this :

    Code:
    +-----QVBoxLayout0--------------+
    |+-QHBoxLayout1--+-------------+|
    ||+-QVBoxLayout2+|QTableWidget0||
    |||QPushButton  ||             ||
    |||QPushButton  ||             ||
    |||stretch      ||             ||
    ||+-------------+|             ||
    |+---------------+--------------+
    |QTableWidget1                  |
    |                               |
    +-------------------------------+
    I can't seem to control the width of either of the QTableWidgets. The only effect I've seen on them is when I call resizeColumnsToContents() which at least makes the contents legible.

    I would like them to completely fill the width of the layout, and I would like the QVBoxLayout1 to be the full width of the screen.

    Also, QTableWidget0, which has a fixed number of rows, gets its bottom chopped off where it goes beyond the layout edge below it. How can I adjust it so that it fits?

    I've tried messing with sizehints and such (QTableWidget1 is actually a class derived from QTableWidget, unlike QTableWidget0), but nothing seems to change at all.

    Max.

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableWidget:how to control size?

    Just to start with something.
    Code:
    #include <QtGui>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTableWidget *w1 = new QTableWidget(10,1);
        for (int i = 0; i < 10; ++i) {
          QTableWidgetItem *item = new QTableWidgetItem(QString("Item %1").arg(i));
          w1->setItem(i, 0, item);
        }
        w1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    
        QTableWidget *w2 = new QTableWidget(10,1);
        for (int i = 0; i < 10; ++i) {
          QTableWidgetItem *item = new QTableWidgetItem(QString("Item %1").arg(i));
          w2->setItem(i, 0, item);
        }
        w2->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    
        QPushButton *b1 = new QPushButton("Button 1");
        b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        QPushButton *b2 = new QPushButton("Button 2");
        b2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(b1, 0, 0, 1, 1);
        layout->addWidget(b2, 1, 0, 1, 1);
        layout->addWidget(w1, 0, 1, 3, 1);
        layout->addWidget(w2, 3, 0, 1, 2);
    
        QWidget w;
        w.setLayout(layout);
        w.show();
    
        return a.exec();
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beijing, China
    Posts
    386

    Re: QTableWidget:how to control size?

    Quote Originally Posted by divanov View Post
    Just to start with something.
    Wow. You went to a lot of trouble...thanks!

    Yeah, your layout looks something like what I've got now.

    Differences are :

    1) I've removed the row headers of both tables with 'table->verticalHeader()->hide()',
    2) I obtained the effect I wanted on the bottom table using 'horizontalHeader()->setStretchLastSection(true);'

    However, it is still disconcerting for my narrow table on the top right to have a scroll bar all the way on the right hand side of the screen. I see from your image that you also have that issue. I would like to see it right next to the table.

    I don't really want the top table to be the whole width - resizing the columns for the data makes them a little small and it looks better with the default (without doing anything, I think). However, when I was playing with it, I tried the 2) above for that table too, but, predictably, it only stretched the last column, while I wanted it to make the two columns take up equal space.

    I noticed you use a grid layout, rather than the v and h box ones. I wonder what happens if I make the bottom table expanding in X. I will play with your example (it's much simpler than my code which does other stuff too), and try to learn.

    Thanks,

    Max.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Beijing, China
    Posts
    386

    Re: QTableWidget:how to control size?

    Quote Originally Posted by davidmaxwaterman View Post
    I wonder what happens if I make the bottom table expanding in X. I will play with your example (it's much simpler than my code which does other stuff too), and try to learn.
    Hrm. I was able to get it compiling and running on the device really quickly

    Unfortunately, I tried the Expanding and Maximum on w1 (I think that's the table in question), but nothing changed. It seems like the same issue.

    I'm sure I'm just missing something quite elementary here...

    Max.

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableWidget:how to control size?

    I have to confess I don't really understand the final goal you are trying to achieve. Is it about making width of QTableWidget column to occupy the whole space of QTableWidget?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Beijing, China
    Posts
    386

    Re: QTableWidget:how to control size?

    Quote Originally Posted by divanov View Post
    I have to confess I don't really understand the final goal you are trying to achieve. Is it about making width of QTableWidget column to occupy the whole space of QTableWidget?
    Yeah, I guess so. Like I say, I just expand the last column, but that only works because there's only one column. If I had more than one, it would look weird...

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: QTableWidget:how to control size?

    I don't have any better ideas:
    main.cpp
    Code:
    #include <QtGui>
    #include <QDebug>
    
    #include "sizedtablewidget.h"
    
    #define ROWS        15
    #define COLUMNS     3
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        SizedTableWidget table(ROWS, COLUMNS);
        table.verticalHeader()->hide();
        for (int i = 0; i < ROWS; ++i) {
          for (int j = 0; j < COLUMNS; ++j) {
            QTableWidgetItem *item = new QTableWidgetItem(QString("Item %1x%2").arg(i).arg(j));
            table.setItem(i, j, item);
          }
        }
        table.show();
    
        return a.exec();
    }
    sizedtablewidget.h
    Code:
    #ifndef SIZED_TABLE_WIDGET_H
    #define SIZED_TABLE_WIDGET_H
    
    #include <QTableWidget>
    #include <QResizeEvent>
    
    class SizedTableWidget : public QTableWidget
    {
        Q_OBJECT
    
    public:
        SizedTableWidget(int rows, int columns, QWidget *parent=0);
    
    protected:
        void resizeEvent(QResizeEvent *event);
    };
    
    #endif //SIZED_TABLE_WIDGET_H
    sizedtablewidget.cpp
    Code:
    #include <QDebug>
    
    #include "sizedtablewidget.h"
    
    SizedTableWidget::SizedTableWidget(int rows, int columns, QWidget *parent)
        : QTableWidget(rows, columns, parent)
    {
    }
    
    void SizedTableWidget::resizeEvent(QResizeEvent *event)
    {
        int width = event->size().width() / columnCount();
        for(int i = 0; i < columnCount(); ++i) {
            setColumnWidth(i, width);
        }
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Beijing, China
    Posts
    386

    Re: QTableWidget:how to control size?

    Quote Originally Posted by divanov View Post
    I don't have any better ideas:
    Well, that seems as good a way as any - perhaps a bit more 'manual' than I had imagined, but anyway.

    Thanks a lot!

    Max.

Similar Threads

  1. changing size and position of multiline dialog
    By pncbose in forum Symbian User Interface
    Replies: 4
    Last Post: 2009-11-04, 01:45
  2. Weird Problem with find box Kern-Exec-3
    By sohilr in forum Symbian
    Replies: 7
    Last Post: 2007-12-05, 04:10
  3. How to change a control size when it becomes focused??
    By aladin3006 in forum Symbian User Interface
    Replies: 2
    Last Post: 2007-05-03, 09:28
  4. User break point error in Grid
    By ag24master in forum Symbian User Interface
    Replies: 0
    Last Post: 2003-08-07, 08:20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×