PDA

View Full Version : QListWidget - inconsistency across operating systems?



scwizard
18th February 2007, 18:06
In native windows API when you select an item from a listbox window the width of the selection highlighting is as wide as the listbox.

In a QListWidget, when you select an item the selection highlighting is only as wide as the text.

Is there anyway I can change the style of a QListWidget to make it look more like a windows listbox?

Here's exactly what I'm talking about (http://img153.imageshack.us/img153/2312/qtstylenh8.png)

jpn
18th February 2007, 19:05
In native windows API when you select an item from a listbox window the width of the selection highlighting is as wide as the listbox.

In a QListWidget, when you select an item the selection highlighting is only as wide as the text.

Is there anyway I can change the style of a QListWidget to make it look more like a windows listbox?

Here's exactly what I'm talking about (http://img153.imageshack.us/img153/2312/qtstylenh8.png)
I think QListView isn't designed too look like that. There is a hackish way to achieve it with a QListView (1), but I'd try with QTreeView (2) instead.

Hackish solution 1:
Override QListWidget::resizeEvent() and make the width of QListView::gridSize() to follow the width of the widget/viewport.

Better solution 2:
A single column QTreeView (or QTreeWidget if you prefer the convenience views) stretches the column to be like that. Hide the header, disable root decoration and add only top level items and voila!

wysota
18th February 2007, 19:16
There is an elegant solution. Just make sure that movement property is set to Static and isWrapping property is set to false. If you insert a QListWidget into Designer, its default properties are set correctly and the whole width of the list is highlighted when selecting.

scwizard
19th February 2007, 03:48
There is an elegant solution. Just make sure that movement property is set to Static and isWrapping property is set to false. If you insert a QListWidget into Designer, its default properties are set correctly and the whole width of the list is highlighted when selecting.
Thanks, but being a Qt newbie I have no idea how to manipulate properties. I read about Qt's Property System (http://doc.trolltech.com/4.2/properties.html), but I don't really understand it.

I did wrote Sidebox->setWrapping(true); but it didn't do anything.
I'm not sure how to setMovement to static.

wysota
19th February 2007, 09:31
Sidebox->setWrapping(false); // I said false, not true
Sidebox->setMovement(QListView::Static);

scwizard
19th February 2007, 16:47
Odd, it still doesn't work. Nothing appears to have changed. Just try compiling the following code:


#include <QtGui>

int main(int argc, char** argv) {
QApplication app(argc, argv);

QListWidget* Sidebox = new QListWidget;

Sidebox->setWrapping(false);
Sidebox->setMovement(QListView::Static);

new QListWidgetItem(QObject::tr("Test"), Sidebox);
new QListWidgetItem(QObject::tr("Hello world"), Sidebox);

Sidebox->show();
return app.exec();
}

Whether or not lines 8 and 9 are included makes no difference, the way the listwidget looks is the same.

jpn
19th February 2007, 16:53
#include <QtGui>

int main(int argc, char** argv) {
QApplication app(argc, argv);

QListWidget* Sidebox = new QListWidget;

Sidebox->setWrapping(false); // <-- _false_
Sidebox->setMovement(QListView::Static);

new QListWidgetItem(QObject::tr("Test"), Sidebox);
new QListWidgetItem(QObject::tr("Hello world"), Sidebox);

Sidebox->show();
return app.exec();
}

Edit: Btw, thanks Wysota for pointing it out! ;)

scwizard
19th February 2007, 17:01
No no no. I tried true and false because neither of them worked. The copy and paste just happened to be from when it was true.

I edited my above post so it would was false. My above post is still correct.

jpn
19th February 2007, 17:07
Which exact version of Qt are you using? Works fine with 4.2.2:

wysota
19th February 2007, 17:11
Sidebox->setWrapping(false); // I said false, not true



Sidebox->setWrapping(false); // <-- _false_



Sidebox->setWrapping(true);

This one is really nice :)


Edit: Btw, thanks Wysota for pointing it out! ;)
Sure, no problem. The question was asked (and answered) before and I personally faced the same problem some time ago... And once you think of it, it's an obvious and logical behaviour for wrapping movable items.



Sidebox->setWrapping(false);
That's why we limit the time to edit own posts... It used to say "true" here a minute ago :)

scwizard
19th February 2007, 17:14
Ok, so the fault isn't in the code. Apparently...

Strange indeed...

This is the code I'm using:
#include <QtGui>

int main(int argc, char** argv) {
QApplication app(argc, argv);
QListWidget* Sidebox = new QListWidget;

Sidebox->setWrapping(false); // <-- _false_
Sidebox->setMovement(QListView::Static);

new QListWidgetItem(QObject::tr("Test"), Sidebox);
new QListWidgetItem(QObject::tr("Hello world"), Sidebox);

Sidebox->show();
return app.exec();
}

jpn says it works with his build of Qt. jpn is using Qt (open source) 4.2 on Linux (as I can see from his screenshot).
I'm using Qt (open source) 4.2 on Windows (like it says to the right of my username). It doesn't work with my build (http://img148.imageshack.us/img148/6144/qtscreenshotzs2.png).

scwizard
19th February 2007, 22:32
I have a favor to ask of the kind people at this forum. Could you copy, paste and compile the following code and give a screenshot of the result (with a list item selected).

I'm asking because jpn (http://www.qtcentre.org/forum/u-jpn-709.html) got this when he compiled the below program (http://www.qtcentre.org/forum/attachment.php?attachmentid=922&d=1171901231). My screenshot and his screenshot have noticeable differences. The selection for his screenshot stretches the length of the widget, but only stretchs the length of the text for mine. My best guess is that the reason for this is that one program was compiled on linux and the other on windows.

The code:

#include <QtGui>

int main(int argc, char** argv) {
QApplication app(argc, argv);
QListWidget* Sidebox = new QListWidget;

Sidebox->setWrapping(false); // <-- _false_
Sidebox->setMovement(QListView::Static);

new QListWidgetItem(QObject::tr("Test"), Sidebox);
new QListWidgetItem(QObject::tr("Hello world"), Sidebox);

Sidebox->show();
return app.exec();
}

The screenshot:

scwizard
19th February 2007, 22:37
Note:
I tested this on two different computer both running windows (XP Home edition) and the latest (4.2.2 open source edition) version of Qt.

Here's the screencapture from my other computer (http://img148.imageshack.us/img148/6144/qtscreenshotzs2.png).

wysota
19th February 2007, 22:38
You can omit the wrapping and movement properties as they are by default set to false and static.

scwizard
19th February 2007, 23:07
@Whichever mod moved and merged:
I'm fine with you merging the topics, but it'd be nice if you'd changed the title of the real topic (as opposed to the move ghost) as "Inconsistency across operating systems?" as that better reflects the actual topic at the moment. I'm apparently not able to do it my self (or edit the first post here at all for that matter).

jacek
19th February 2007, 23:30
Try running your application with Plastique style: "app -style plastique"

wysota
19th February 2007, 23:53
Cleanlooks works as well.

scwizard
20th February 2007, 00:05
Those looks quite nice. I think I may end up just changing the style of the application if there is no way to get listwidget item selections to span the length of the widget in a windows style.

jacek
20th February 2007, 00:09
if there is no way to get listwidget item selections to span the length of the widget in a windows style.
At least there is no inconsistency across operating systems --- just the windows style behave a bit differently (on Linux too).

Have you tried the windowsxp style?

wysota
20th February 2007, 01:55
just the windows style behave a bit differently (on Linux too).

All motif-like styles (motif, cde, windows) behave that way. You can always make a proxy style to correct(*) that behaviour in a style independent way.

---
(*) - change

scwizard
20th February 2007, 02:45
Thanks for all the help everybody. What I'm doing is I'm calling setStyle for the listbox and setting it to cleanlooks. I might toy around with custom styles in the future, but my immediate problem has been solved.