Results 1 to 7 of 7

Thread: Resizing QSqlTableModel

  1. #1
    Join Date
    Jan 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Resizing QSqlTableModel

    Hi.
    Is there any way to resize QSqlTableModel (I want to auto resize colums), like this:

    http://img58.imageshack.us/img58/1092/qstableres1oc.png

    and resize it to this:

    http://img58.imageshack.us/img58/826...bleres26kj.png


    Thanks in Advance for any help.

  2. #2
    Join Date
    Jan 2006
    Location
    England
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing QSqlTableModel

    For starters, you won't resize the model but rather the table widget you're using to display it (see http://doc.trolltech.com/4.1/qt4-interview.html). This could either be a QTableView or QTableWidget. Get the horizontal header of the table using
    Qt Code:
    1. QHeaderView* QTableView::horizontalHeader();
    To copy to clipboard, switch view to plain text mode 
    And resize the section you want using
    Qt Code:
    1. void QHeaderView::resizeSection(int logicalindex, int size);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing QSqlTableModel

    Thanks, but I don't realy know how to use it.

    I'm creating my QTableView in function Okno::baza() :

    Qt Code:
    1. db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("magazyn");
    3. db.open();
    4. tabelka = new QSqlTableModel;
    5. tabelka->setTable("produkty");
    6. tabelka->select();
    7. tabelka->setHeaderData(0, Qt::Horizontal, tr("Id"));
    8. tabelka->setHeaderData(1, Qt::Horizontal, tr("Nazwa"));
    9. tabelka->setHeaderData(2, Qt::Horizontal, tr("Ilość"));
    10. tabelka->setHeaderData(3, Qt::Horizontal, tr("Cena Netto"));
    11. tabelka->setHeaderData(4, Qt::Horizontal, tr("Data Ważności"));
    12. view = new QTableView;
    13. view->setModel(tabelka);
    To copy to clipboard, switch view to plain text mode 

    In constructor Okno::Okno() I'm adding view do layout:

    Qt Code:
    1. baza();
    2. centralWidget = new QWidget;
    3. glowny = new QVBoxLayout;
    4. glowny->addWidget(view, 0, Qt::AlignHCenter);
    5. centralWidget->setLayout(glowny);
    6. setCentralWidget(centralWidget);
    To copy to clipboard, switch view to plain text mode 

    Class Okno inherits from QMainWindow.
    Last edited by teS; 12th January 2006 at 22:00.

  4. #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: Resizing QSqlTableModel

    Quote Originally Posted by Corran
    This could either be a QTableView or QTableWidget.
    It can't be QTableWidget. Its setModel() method is private. It can probably be QTreeView, though.

  5. #5
    Join Date
    Jan 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing QSqlTableModel

    Sorry for refreshing this topic, but I haven't found sloution for my problem yet.
    So I'm asking again: Is there any way to resize colums in QTableView ?

  6. #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: Resizing QSqlTableModel

    I think the question has already been answered in the second post of this thread.

  7. #7
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing QSqlTableModel

    Quote Originally Posted by teS
    Sorry for refreshing this topic, but I haven't found sloution for my problem yet.
    So I'm asking again: Is there any way to resize colums in QTableView ?
    Qt Code:
    1. QSqlQueryModel *viewModel = new QSqlQueryModel(wnui.wholeNameView);
    2. viewModel->setHeaderData(0, Qt::Horizontal, "ID");
    3. viewModel->setHeaderData(1, Qt::Horizontal, "SSN");
    4. viewModel->setHeaderData(2, Qt::Horizontal, "WholeName");
    5. viewModel->setHeaderData(3, Qt::Horizontal, "SSSN");
    6. viewModel->setHeaderData(4, Qt::Horizontal, "SName");
    7. viewModel->setHeaderData(5, Qt::Horizontal, "City");
    8. viewModel->setHeaderData(6, Qt::Horizontal, "Address");
    9.  
    10. viewModel->setQuery(queryStr);
    11. if (viewModel->lastError().type() == QSqlError::NoError){
    12. wnui.wholeNameView->setModel(viewModel);
    13. if (viewModel->rowCount() > 0){
    14. for (int i = 0; i < viewModel->rowCount(); ++i)
    15. this->wnui.wholeNameView->verticalHeader()->resizeSection(i,20);
    16. for (int i = 0; i < 7; ++i)
    17. wnui.wholeNameView->resizeColumnToContents(i);
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    where wholeNameView is the name of the QTableView object in my wholenamedlg.ui file. "wnui" was the name assigned to the Ui class in the wholenamedlg class definition in the wholenamedlg.h file, and wholeNameDlgUI is the ObjectName of the wholenamedlg.ui form itself.
    Qt Code:
    1. private:
    2. Ui::wholeNameDlgUI wnui;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  2. Replies: 4
    Last Post: 9th May 2008, 17:02
  3. Replies: 2
    Last Post: 14th August 2007, 15:16
  4. QMdiSubWindow resizing in 4.3
    By kalpa in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2007, 13:39
  5. QSqlTableModel cannot change values
    By raphaelf in forum Newbie
    Replies: 2
    Last Post: 23rd May 2007, 08:01

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.