Results 1 to 8 of 8

Thread: QComboBox with QSqlQueryModel

  1. #1
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QComboBox with QSqlQueryModel

    I've set sizeAdjustPolicy of QComboBox to AdjustToContents but with QSqlQueryModel it doesn't work - long items is represented as lon...em for example but not longitem as expected. Is there any solution for this?

    Qt 4.2.3 using under WinXpPro and Linux
    Last edited by L.Marvell; 22nd May 2007 at 13:49.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlQueryModel

    I would reimplement QAbstractItemDelegate and return the required sizeHint for that item.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox with QSqlQueryModel

    Quote Originally Posted by L.Marvell View Post
    I've set sizeAdjustPolicy of QComboBox to AdjustToContents but with QSqlQueryModel it doesn't work - long items is represented as lon...em for example but not longitem as expected. Is there any solution for this?
    Would you mind attaching a minimal compilable example reproducing the problem? According to QComboBox / QSqlQueryModel sources the model should inform the change and the combo should recalculate its size hint.

    Quote Originally Posted by marcel View Post
    I would reimplement QAbstractItemDelegate and return the required sizeHint for that item.
    I'm afraid that would only affect the size of the popup view. QComboBox calculates its size hint based on item texts.
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlQueryModel

    Quote Originally Posted by jpn View Post
    Would you mind attaching a minimal compilable example reproducing the problem?
    I didn't do anything special. I have the ui with QComboBox with SizeAdjustPolicy set to adjustToContents (not on first show)
    Qt Code:
    1. modelCategories = new QSqlQueryModel;
    2. ...
    3. // connecting to PostgreSQL database
    4. ...
    5. modelCategories->setQuery("SELECT category_name FROM mytable");
    6. comboBox->setModel(modelCategories);
    To copy to clipboard, switch view to plain text mode 
    And so I have described problem. But if I create items for each query result and add them to combo all works fine.

    Quote Originally Posted by jpn View Post
    I'm afraid that would only affect the size of the popup view. QComboBox calculates its size hint based on item texts.

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox with QSqlQueryModel

    Quote Originally Posted by L.Marvell View Post
    I didn't do anything special. I have the ui with QComboBox with SizeAdjustPolicy set to adjustToContents (not on first show)
    Qt Code:
    1. modelCategories = new QSqlQueryModel;
    2. ...
    3. // connecting to PostgreSQL database
    4. ...
    5. modelCategories->setQuery("SELECT category_name FROM mytable");
    6. comboBox->setModel(modelCategories);
    To copy to clipboard, switch view to plain text mode 
    There's not much I can do with that piece of code. I wanted to test and see myself, that's why I asked for something compilable to test with.

    I took one of the SQL examples shipped with Qt and modified it to use combo boxes instead of tables. The attached example seems to work pretty fine for me. Does it work for you?
    Attached Files Attached Files
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlQueryModel

    Quote Originally Posted by jpn View Post
    There's not much I can do with that piece of code. I wanted to test and see myself, that's why I asked for something compilable to test with.

    I took one of the SQL examples shipped with Qt and modified it to use combo boxes instead of tables. The attached example seems to work pretty fine for me. Does it work for you?
    Sorry, I have no enough experience to write quickly such code as yours. Ok, I've rewrite it a little (see in attach) and I think the possible problem is that I use spacer in ui form but now have no time to check this version. What do you think about?
    Attached Files Attached Files

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox with QSqlQueryModel

    Quote Originally Posted by L.Marvell View Post
    What do you think about?
    Ok, that's an interesting bug..

    QComboBox caches its size hint. The combo box of yours ends up calculating its size hint when the designed form is loaded:
    Qt Code:
    1. QSize size(400, 318);
    2. size = size.expandedTo(MainWindow->minimumSizeHint()); // thanks to this line in generated ui_mainwindow.h
    3. MainWindow->resize(size);
    To copy to clipboard, switch view to plain text mode 

    QComboBox should invalidate it's size hint when a new model is set. Because it doesn't, the combo remains at cached size which was calculated when there were no items at all (defaults to width of 7 'x' characters).

    A workaround is to change the order of statements like this:
    Qt Code:
    1. QSqlQueryModel model; // do not set the query yet
    2.  
    3. MyMainWindow window; // let combo box calculate its initial size hint (designed form is loaded in MyMainWindow constructor)
    4. window.combobox()->setModel(&model); // this does not cause size hint recalculation
    5. model.setQuery("select * from person"); // but this one does (data of the model changes)
    6. window.combobox()->setModelColumn(2);
    7. window.combobox()->setCurrentIndex(0);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    L.Marvell (23rd May 2007)

  9. #8
    Join Date
    Jan 2006
    Location
    Ukraine, L'viv
    Posts
    57
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with QSqlQueryModel

    Quote Originally Posted by jpn View Post
    A workaround is to change the order of statements...
    Yes, in this case it works perfect. Thanks for help!

Similar Threads

  1. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14
  2. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 10:50
  3. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  4. QcomboBox items
    By therealjag in forum Newbie
    Replies: 5
    Last Post: 27th March 2006, 08:21
  5. QComboBox +SUSE10.0 +qt4.1 strange behavior
    By antonio.r.tome in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2006, 17:49

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.