Hi,
I am writing a preferences dialog that has a QListWidget on the left side and the settings buried in QTabWidgets and QWidgets positioned to the right of the listwidget. Now, when I open the prefs dialog, it divides the size of the dialog equally between the two widgets, but that is not what I want. I want the size of the QListWidget to be the size of the maximum QListWidgetItem it contains + a few percent. and resizing the dialog should give every extra space to the QTabWidgets/QWidgets. I can achieve this by setting the QListWidgets size to some static value like "120". This however should be problematic, if someone uses larger fonts etc. iterating through the QListWidgetItems and getting the maximum width by using QFontMetrics is also not the best solution since the outcome is rendered useless if I add a QIcon to the QListWidgetItem.
// set Size
int maxTextWidth;
for (int i=0; i<listWidget->count(); i++) {
qDebug() << listWidget->item(i)->sizeHint().width();
int x;
if ( (x = fm.width(listWidget->item(i)->text())) > maxTextWidth )
maxTextWidth = x;
};
listWidget->setMaximumWidth( maxTextWidth + maxTextWidth * 0.3 ); // add 30 percent to the size
// set Size
QFontMetrics fm( font() );
int maxTextWidth;
for (int i=0; i<listWidget->count(); i++) {
qDebug() << listWidget->item(i)->sizeHint().width();
int x;
if ( (x = fm.width(listWidget->item(i)->text())) > maxTextWidth )
maxTextWidth = x;
};
listWidget->setMaximumWidth( maxTextWidth + maxTextWidth * 0.3 ); // add 30 percent to the size
To copy to clipboard, switch view to plain text mode
The Items sizeHint() function returns -1 so I can't use it either. Can anyone tell me how to do it the right way?
Thanx in advance
momesana
Bookmarks