Results 1 to 16 of 16

Thread: Cannot resize QTableView within a QWidget

  1. #1
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Cannot resize QTableView within a QWidget

    Hi,

    I've got quite annoying problem. I've put a QTableView in a QWidget that works as an another window. The problem is that QTableViewdoes not resize itself when I resize a window. Also, the window starts with its minimum geometry as if there was none of any layouts at all even though the QTableView is inside. So far, I tried to put QTableView in the one of the layouts: QGridLayout, QHBoxLayout etc. but it does not solve the problem. Am I doing something wrong here? I did not have such a problem before; all layouts with widgets worked great for me.

    Just in case I put the part of the code that is responsible for opening the window.

    Qt Code:
    1. void MyApp::showTabulatedEvents() {
    2. const int maxRows = dbStoreToday.size();
    3.  
    4. upcomingEventsForToday(); // ustaw liste dbStoreToday
    5.  
    6. QWidget* tableWindow = new QWidget;
    7. QVBoxLayout* tableLay = new QVBoxLayout(tableWindow);
    8. tableWindow->setWindowTitle(trUtf8("Upcoming events for today"));
    9. QTableView* tableView = new QTableView;
    10. tableLay->addWidget(tableView);
    11. tableView->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    12.  
    13. QStandardItemModel* model = new QStandardItemModel(5,dbStoreToday.size());
    14. for(int row = 0; row < maxRows; ++row) {
    15. model->setItem(row,0,new QStandardItem(dbStoreToday[row].imie));
    16. model->setItem(row,1,new QStandardItem(dbStoreToday[row].nazwisko));
    17. model->setItem(row,2,new QStandardItem(dbStoreToday[row].nazwa_wyd));
    18. model->setItem(row,3,new QStandardItem(dbStoreToday[row].data_wyd.toString()));
    19. model->setItem(row,4,new QStandardItem(tr("%1").arg(dbStoreToday[row].przypomnienie)));
    20. }
    21. tableView->setModel(model);
    22. tableView->show();
    23. tableWindow->show();
    24. }
    To copy to clipboard, switch view to plain text mode 

    tableView.jpg

  2. #2
    Join Date
    Aug 2012
    Location
    Montreal
    Posts
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot resize QTableView within a QWidget

    I tried the code you posted and it works fine, the table resizes with the window. Could you provide additional information? Qt version? Os version? Have you tried using the design editor in QCreator?

    I used Qt 4.8.0 for MSVC2010.

  3. #3
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    Hi Maximebd,

    I am using QCreator but I have compiled Qt 4.8.2. I am quite surprised it's working for you. May I ask how you mean you have tried my code? I am asking because as you see this is only a function inside my fairly large project. This function is simply supposed to create another window (not the main window) with QTableView inside. Can you post the exact code you have written even if it's using my function. I would be grateful.

    Thanks

  4. #4
    Join Date
    Aug 2012
    Location
    Montreal
    Posts
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot resize QTableView within a QWidget

    Here is the code I compiled and ran:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QWidget* tableWindow = new QWidget();
    8. QVBoxLayout* tableLay = new QVBoxLayout(tableWindow);
    9. tableWindow->setWindowTitle("tralaa");
    10. QTableView* tableView = new QTableView();
    11. tableLay->addWidget(tableView);
    12. tableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    13.  
    14. for (int r = 0; r < 5; ++r)
    15. {
    16. for (int c = 0; c < 5; ++c)
    17. {
    18. model->setItem(r, c, new QStandardItem(QString::number(r*10 + c)));
    19. }
    20. }
    21.  
    22. tableView->setModel(model);
    23. tableView->show();
    24. tableWindow->show();
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cannot resize QTableView within a QWidget

    If something is not resizing correctly in Qt it is almost certainly because the layouts are broken, missing, or not applied.

    You put the table widget into a layout but never apply the layout to the container widget (and maximebd does the same) so the two are not related in any automatic way. Here is a minimal example:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Widget: public QWidget {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *p = 0): QWidget(p) {
    8. QLabel *label = new QLabel("Just filling space for example", this);
    9. QTableWidget *table = new QTableWidget(10, 5, this);
    10.  
    11. QVBoxLayout *layout = new QVBoxLayout(this);
    12. layout->addWidget(label);
    13. layout->addWidget(table);
    14. setLayout(layout); // <<< the magic sauce
    15. }
    16. public slots:
    17. private:
    18. };
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. QApplication app(argc, argv);
    23.  
    24. Widget m;
    25. m.show();
    26. return app.exec();
    27. }
    28. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    Hi Chris

    I tried maximebd's code and indeed I obtained essentially the same result but the window is larger by default. I also, to be honest, did try to put the line
    Qt Code:
    1. tableWindow->setLayout(tableLay);
    To copy to clipboard, switch view to plain text mode 
    but it did not make any change. I am wondering why it did not make connection of this layout with the widget. Perhaps I need to make another class representing my window the way you have shown in your post. I'll try to follow this way but I am not sure if it's how it should be.

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Cannot resize QTableView within a QWidget

    From the QWidget::setLayout() docs:
    An alternative to calling this function is to pass this widget to the layout's constructor.
    . . . which is what you have done.

    You'll get the default minimum size for a widget unless you use QWidget::setMinimumSize() or subclass
    QTableView, re-implement minimunSizeHint() and calculate the size. Check out the last post in this thread. Note that the code posted there does not set limits on the width nor the height (a maximum minimum, if you will) so you could end up with a widget larger than your screen and no way to move around.

  8. #8
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    @norobro
    Hi norobro,

    Thanks for this post. I will definitely try it.

    ----------
    @Chris.
    Unfortunately,
    Qt Code:
    1. setLayout(tableLay);
    To copy to clipboard, switch view to plain text mode 
    is not working.
    I did also try to inherit from QWidget, like I would make a single application, and add QTableView widget to it and set everything in roughly similar way to you in your last post. It also did not work. I have an impression I did everything like you did in your post. Yet, the result is exactly the same as before. There are two things that really are bothering me. First is that nothing I do cause any effect. The second is that I also set a title of that extra window with a table. Surprisingly, the title is the same as the title of my application application.

    This is the code of the new class:

    tableview.h
    Qt Code:
    1. #ifndef TABLEVIEW_H
    2. #define TABLEVIEW_H
    3.  
    4. #include <QWidget>
    5. #include <QDialog>
    6. #include <QVBoxLayout>
    7. #include <QList>
    8. #include <QTableView>
    9. #include "../stworz_plik/rekord.h"
    10.  
    11. class TableView : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. private:
    16. QTableView* tableView;
    17. QVBoxLayout* tableLay;
    18.  
    19. public:
    20. explicit TableView(const QList<Wydarzenia::Rekord>& dbStoreToday, QObject *parent = 0);
    21.  
    22. signals:
    23.  
    24. public slots:
    25.  
    26. };
    27.  
    28. #endif // TABLEVIEW_H
    To copy to clipboard, switch view to plain text mode 

    and tableview.cpp
    Qt Code:
    1. #include <QStandardItem>
    2. #include <QStandardItemModel>
    3. #include <QStringList>
    4. #include <QStandardItem>
    5. #include <QStandardItemModel>
    6. #include <QStringList>
    7. #include <QLabel>
    8. #include "tableview.h"
    9.  
    10. TableView::TableView(const QList<Wydarzenia::Rekord> &dbStoreToday, QObject *parent)
    11. : QWidget(parent) {
    12.  
    13. const int maxRows = dbStoreToday.size();
    14.  
    15. tableView = new QTableView(this);
    16. tableView->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    17.  
    18. tableLay = new QVBoxLayout(this);
    19. tableLay->addWidget(tableView);
    20. tableLay->addWidget(new QLabel("aaa",this));
    21.  
    22. QStandardItemModel* model = new QStandardItemModel(maxRows,5,this);
    23. model->setHorizontalHeaderItem(0, new QStandardItem(trUtf8("Name")));
    24. model->setHorizontalHeaderItem(1, new QStandardItem(trUtf8("Surname")));
    25. model->setHorizontalHeaderItem(2, new QStandardItem(trUtf8("Event name")));
    26. model->setHorizontalHeaderItem(3, new QStandardItem(trUtf8("Event date")));
    27. model->setHorizontalHeaderItem(4, new QStandardItem(trUtf8("Remainder")));
    28. for(int row = 0; row < maxRows; ++row) {
    29. QStringList itemList;
    30. itemList << dbStoreToday[row].imie << dbStoreToday[row].nazwisko << dbStoreToday[row].nazwa_wyd
    31. << dbStoreToday[row].data_wyd.toString() << tr("%1").arg(dbStoreToday[row].przypomnienie);
    32. for(int col = 0; col < 5; ++col) {
    33. QStandardItem* item = new QStandardItem(itemList[col]);
    34. item->setEditable(false);
    35. model->setItem(row,col,item);
    36. }
    37. }
    38. tableView->setModel(model);
    39. tableView->show();
    40. setLayout(tableLay);
    41. }
    To copy to clipboard, switch view to plain text mode 

    and the new function is as follows:
    Qt Code:
    1. void MyApp::showTabulatedEvents() {
    2. upcomingEventsForToday(); // ustaw liste dbStoreToday
    3. TableView* tableView = new TableView(dbStoreToday,this);
    4. tableView->setWindowTitle("Upcoming events");
    5. tableView->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    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: Cannot resize QTableView within a QWidget

    Could you please prepare a minimal compilable example reproducing the problem?
    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.


  10. #10
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    Hello wysota,

    Unfortunately, I could not reproduce this issue. I did prepare a simple application, let's call it "Simple Application" that creates a window with two buttons: "Show" and "Close". When "Show" is clicked, another window appears with a table. In "Simple Application", everything works as expected meaning the window opens with a default geometry (not with minimal size showing only a title bar). It only shows a part of a table but scroll bars are placed in the window not in the QTableView widget; that is, the whole table can be shown when the window is resized.

    Obviously, as the function worked here, I tried to changed the main function in my application so that I could obtain the same effect. It did not work. I made one more try and copied everything. When I copied / pasted the whole content of MainWindow::showTable() from "Simple Application" to MyApp::showTabulatedEvents() in the main application and overwritten it, the QTableWidget was trimmed again and the issue still remains.
    I am wondering what may cause this issue but this is far too much complex problem. Perhaps I can say more about application ...
    The main application creates two timers, a system tray icon, a menu applied to the system tray icon, and connections. The first QTimer is just to check for an existing file once the event loop is in place. Once the timeout() is emitted, the timer is deleted. Another timer triggers a signal that is connected to a function which shows information in a balloon. The application creates a trayIconMenu, a few actions, and connect SIGNALs of each action triggered() function with SLOTs. Because some information are too long I tried to let user see tabulated information; that is, one of the actions from menu makes MyApp::showTabulatedEvents() call that should show information in table.
    I was also wondering if I should make a simple application with system tray icon but that would take a bit more space here.

    EDIT: One more thing. The program has its main window that can be used to make settings. At the moment, it only set up the time per which notification appears in system tray periodically. The main window is closed by default. I am wondering if this may cause the problem because if the main window is closed, then another window becomes the main window.


    main.cpp in Simple Application
    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char* argv[]) {
    5. QApplication app(argc,argv);
    6.  
    7. MainWindow mwin;
    8. mwin.setWindowTitle("The main window's title");
    9. mwin.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h in Simple Application
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QtGui>
    4.  
    5. class MainWindow : public QWidget {
    6. Q_OBJECT
    7. public:
    8. explicit MainWindow(QWidget *parent = 0);
    9. signals:
    10. public slots:
    11. void showTable();
    12. };
    13.  
    14. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp in Simple Application
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QWidget(parent) {
    5.  
    6. QHBoxLayout* hBoxLay = new QHBoxLayout;
    7. QPushButton* showButton = new QPushButton(tr("Show"));
    8. QPushButton* closeButton = new QPushButton(tr("Close"));
    9. hBoxLay->addWidget(showButton);
    10. hBoxLay->addWidget(closeButton);
    11. setLayout(hBoxLay);
    12.  
    13. connect(closeButton,SIGNAL(clicked()),qApp,SLOT(quit()));
    14. connect(showButton,SIGNAL(clicked()),this,SLOT(showTable()));
    15. }
    16.  
    17. void MainWindow::showTable() {
    18. QWidget* tableWindow = new QWidget;
    19. tableWindow->setWindowTitle("Another title");
    20.  
    21. QGridLayout* tableLay = new QGridLayout(tableWindow);
    22. QTableView* tableView = new QTableView;
    23. tableLay->addWidget(tableView);
    24.  
    25. tableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    26. QStandardItemModel* model = new QStandardItemModel(5, 5, tableWindow);
    27. for (int row = 0; row < 5; ++row) {
    28. for (int col = 0; col < 5; ++col) {
    29. QStandardItem* item = new QStandardItem(tr("Row: %1, Col: %2").arg(row).arg(col));
    30. item->setEditable(false);
    31. model->setItem(row, col, item);
    32. }
    33. }
    34. tableView->setModel(model);
    35. tableView->show();
    36.  
    37. tableWindow->setLayout(tableLay);
    38. tableWindow->show();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ZikO; 8th September 2012 at 13:42.

  11. #11
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    Ok,

    I am devastated I cannot understand the behaviour of the Qt.

    There was no way to make QTableView works so I created a new project and just in case tested the QTableView within a new window whether it works before anything else. I put just a testing title "Another title" and some arbitrary coordinates 400,300. Because QTableView was rendered correctly, I copied pasted everything into new project. Then, I tried to change the title back and increase size of the window with QTableView but I cannot. The issue is exactly the same with that difference that now I have setLayout applied as it was applied first time and the ir works. However, I cannot change title, coordinates, size, nothing. This looks to me like Qt does not refresh information even between compilations. I did try to Clean All and rebuild app, I also removed all Makefile(s) but it does not change the window parameters. What else can I do? This issue is really strange.

    Is there any place that Qt keeps QWidget information?


    Added after 17 minutes:


    I removed all files such as *.rcc, *.qrc, and *.rc and I can finally see the app changes between compilations. I have also removed completely Debug folder. I cannot say what has helped. It's a strange issue. I still don't know whether I caused that myself or whether it is a bug.
    Last edited by ZikO; 8th September 2012 at 22:22.

  12. #12
    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: Cannot resize QTableView within a QWidget

    Quote Originally Posted by ZikO View Post
    This looks to me like Qt does not refresh information even between compilations.
    Qt is not a vicious man pulling strings to ruin your application. It is an application development framework, a simple set of libraries. It does not do anything to your compilation. If rebuilding the project from scratch doesn't help to reflect changes you make in a file then the most obvious reason is that you are changing the wrong file.

    If you can't reproduce your problem with a simple example, then apparently the problem is not in the part of code illustrated by the example which implies that you are probably looking for the bug in the wrong place.
    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.


  13. #13
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    Because I am a beginner, I really cannot precisely indicate the issue. I wish I could. However, I did my best to provide all my observations and results.
    It's definitely the last thing I would discuss or argue with someone with strong expertise in this area. However, what I am saying is that the issue was repeated in two cases when I rebuild the application. I did not blindly copy / paste everything. As I said I was testing new app progressively. Whether this matter is taken seriously or not, I know where I should search the issue if this happens again. I explained when it started working. I cannot however be certain and answer if changes a QWidget object parameters should take the effect after next compilation always. I used a Qt library and I changed its parameters so IMO it should have something to do with Qt. I really believe that changes should take effect immediately after compilation. And this is why I wrote two last posts.

  14. #14
    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: Cannot resize QTableView within a QWidget

    If you change a file containing Qt code and start the build process, "Qt" doesn't "touch" your file, the C++ compiler does. It is completely no difference whether your project uses Qt or not. If changes made in the file are not reflected then it means the file was not recompiled or that what you see doesn't come from the file you thought it comes from. Sometimes people have two files with the same name and simply change the wrong one or they have two copies of code producing some result in their project and they are changing one but then looking at results of the other. To verify whether any of these is the case, the simplest way is to make a syntax error in the file. If the project builds afterwards then it means the compiler didn't even parse the source file (again, Qt having nothing to do with that as it is only used runtime).

    The code in your initial post looks correct and should provide a valid layout setup.
    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.


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

    ZikO (10th September 2012)

  16. #15
    Join Date
    Nov 2009
    Posts
    61
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Cannot resize QTableView within a QWidget

    To verify whether any of these is the case, the simplest way is to make a syntax error in the file. If the project builds afterwards then it means the compiler didn't even parse the source file (again, Qt having nothing to do with that as it is only used runtime).

    The code in your initial post looks correct and should provide a valid layout setup.
    Hello,

    Thank you for this post. I appreciate the effort in providing the explanation and I am happy with that clue; it is actually very practical one. I'll do it next time when that issue occurs again, if it occurs again.

  17. #16
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Cannot resize QTableView within a QWidget

    in qtcreator... choose frame and drop in form..right click on the form and choose set layout as vertical and drop ur tableview in that frame. right click in that frame and choose layout as horizontal... it will resize automatically...let me know how it works

Similar Threads

  1. how to resize Qmainwindow with Qwidget ?
    By zeynepb.bil in forum Qt Programming
    Replies: 10
    Last Post: 28th September 2017, 23:48
  2. Replies: 1
    Last Post: 1st May 2010, 23:03
  3. Replies: 3
    Last Post: 1st April 2010, 23:56
  4. Qwidget resize delay problem
    By zl2k in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2008, 14:02
  5. Resize QWidget in QMainWindow
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2007, 12:16

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.