PDA

View Full Version : QComboBox with QSqlQueryModel



L.Marvell
22nd May 2007, 13:10
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

marcel
22nd May 2007, 17:01
I would reimplement QAbstractItemDelegate and return the required sizeHint for that item.

jpn
22nd May 2007, 19:54
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. :)


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.

L.Marvell
23rd May 2007, 10:09
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)


modelCategories = new QSqlQueryModel;
...
// connecting to PostgreSQL database
...
modelCategories->setQuery("SELECT category_name FROM mytable");
comboBox->setModel(modelCategories);

And so I have described problem. But if I create items for each query result and add them to combo all works fine.


I'm afraid that would only affect the size of the popup view. QComboBox calculates its size hint based on item texts.

jpn
23rd May 2007, 10:53
I didn't do anything special. I have the ui with QComboBox with SizeAdjustPolicy set to adjustToContents (not on first show)


modelCategories = new QSqlQueryModel;
...
// connecting to PostgreSQL database
...
modelCategories->setQuery("SELECT category_name FROM mytable");
comboBox->setModel(modelCategories);

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?

L.Marvell
23rd May 2007, 14:12
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?

jpn
23rd May 2007, 14:38
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:


QSize size(400, 318);
size = size.expandedTo(MainWindow->minimumSizeHint()); // thanks to this line in generated ui_mainwindow.h
MainWindow->resize(size);


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:


QSqlQueryModel model; // do not set the query yet

MyMainWindow window; // let combo box calculate its initial size hint (designed form is loaded in MyMainWindow constructor)
window.combobox()->setModel(&model); // this does not cause size hint recalculation
model.setQuery("select * from person"); // but this one does (data of the model changes)
window.combobox()->setModelColumn(2);
window.combobox()->setCurrentIndex(0);

L.Marvell
23rd May 2007, 14:52
A workaround is to change the order of statements...


Yes, in this case it works perfect. Thanks for help!