PDA

View Full Version : QWidget orientation



Valheru
11th August 2008, 10:25
How can you dynamically set the orientation of a widget? I am implementing a QDockWidget with a widget for it's header, and I need to set up the header widget so that when the header orientation is changed in the dock widget, it changes the orientation of it's children (a label and an icon). The text will need to be rendered on it's side( rotated by either 90 or -90 degrees). The Qt doc's aren't very helpful in this regard I'm afraid.

Thanks in advance.

wysota
11th August 2008, 10:43
What exactly do you mean by "set the orientation"? You mean support of right-to-left languages or something else? If it's just about repositioning child widgets on the parent widget, simply change the layout from vertical to horizontal or the other way round.

Valheru
11th August 2008, 14:42
Left-to-right or vice-versa is defined as the "layout direction" in Qt. The "orientation" is if the widget is vertically or horizontally orientated. But apart from that caveat I do mean the latter of what you said :p
QWidgets have not setLayout(), however :( They do have a setLayoutDirection(), but that is to support right-to-left languages.

Valheru
11th August 2008, 19:26
I've been going through the docs, and I can't for the life of me figure out how to do this :( Some widgets have it, QHeaderView and QSplitter for example, but not normal widgets :(

wysota
11th August 2008, 20:34
Ok, but what do you want to do? Change the orientation in what manner? From horizontal to vertical, right to left or what? Have you tried the solution I gave you? Do you want to rotate the widgets or just change their layout on the parent?

Valheru
11th August 2008, 21:11
Look, I can't get any clearer than I have been. I have a widget, which is a title bar - not that it matter. There is text written on it - left to right. Now when the dock widget's header orientation is set to vertical, you need to draw the title widget in a vertical orientation. That is, the title widget's size has to change, and the text has to be written on it's side. I've mentioned all of this in the above posts. And your abovementioned solution was too vague to work with - there is no "setOrientation" function in a widget. Here's a picture for you:



-----------------------------------------------
| Some text |
|________________________________|needs to become this:



--------
| |
| |
| |
| t |
| x |
| e |
| T |
| |
| |
| |
--------
In the above pic the text is wrong though, that needs to be rotated by 270 degrees.

/edit: Ok, the forum is messing up my pretty ASCII art :p And that is just the titlebar BTW, that is also in a widget.
If you can't follow me, just make a quick mockup of a QDockWidget in a QMainWindow in designer, and connect a button so that you can call the setFeatures function so that you can switch on the i { white-space: pre-wrap; } QDockWidget::DockWidgetVerticalTitleBar feature. Make sure the widget is docked on either the left or the right and you will see what I am trying to do.

wysota
12th August 2008, 02:47
Look, I can't get any clearer than I have been.
Sure you can. You just did in the last post.


I have a widget, which is a title bar - not that it matter. There is text written on it - left to right. Now when the dock widget's header orientation is set to vertical, you need to draw the title widget in a vertical orientation. That is, the title widget's size has to change, and the text has to be written on it's side.
See? Nothing to it.


I've mentioned all of this in the above posts.
Well, obviously you were not clear enough :)


And your abovementioned solution was too vague to work with - there is no "setOrientation" function in a widget.
Please show me where did I tell you to use some "setOrientation".



/edit: Ok, the forum is messing up my pretty ASCII art :p
Not the forum, the proportional vs fixed font difference on your computer.

Ok, enough flamewar, now for the solution.... there are three possibilities.

1. Use a QGraphicsView with a proxy widget and you'll be able to rotate the widget within the view as you want.
2. Subclass each widget you want transposed and reimplement its paint event (and probably at least also sizeHint and mouse event handlers if they are needed) where you will rotate the painter before drawing the actual widget. There is an example in our wiki showing how to obtain a rotated button.
3. If you just need the looks, use QWidget::grabPixmap() and use the pixmap as the rotated widget.

Valheru
18th August 2008, 12:01
I've been trying on and off to get this to work by subclassing the widget and reimplementing the paintEvent, using the wiki pushbutton article as a reference. However, when my widgets are orientated vertically, they display no icon or text.

Could someone please take a look at the following working example and tell me what I am doing wrong? You will need Qt 4.4 and CMake 2.4 or higher to compile it. Note that unless you are running Linux with the Qt designer icon in /usr/share/pixmaps, you may have to edit mainwindow.ui to get the icon (or any other of your choice) to show up.

TIA,
Valheru

Valheru
19th August 2008, 18:20
Bump :p I still can't get this to work. It's as though the coordinates are wrong, making the widgets paint offscreen, but I can't see how that could be.