You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

boyz5

Unregistered

1

Monday, December 6th 2004, 4:11pm

QComboBox in QToolbar ???

hello,
I was wondering how I can set a QComboBox in the QToolBar. I already tried this:

QComboBox *cb = new QComboBox(this);
QAction *action = new QAction("a combobox", 0, cb);
action->addTo(MyToolbar);

But the combobox is not exactly in the toolbar- it's somewhere else placed :D

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

2

Monday, December 6th 2004, 5:09pm

RE: QComboBox in QToolbar ???

I don't think QAction has a constructor that fits your arguments -- but it's not what you need anyway.

Take a look at the documentation for QToolBar and its parent QDockWindow:

Quoted

A dock window is often used to contain a single widget. In these cases the widget can be set by calling setWidget(). If you're constructing a dock window that contains multiple widgets, e.g. a toolbar, arrange the widgets within a box layout inside the dock window. To do this use the boxLayout() function to get a pointer to the dock window's box layout, then add widgets to the layout using the box layout's QBoxLayout::addWidget() function. The dock window will dynamically set the orientation of the layout to be vertical or horizontal as necessary, although you can control this yourself with setOrientation().


I don't think you can just create the combo as a child of the toolbar.

Sam

This post has been edited 1 times, last edit by "samdutton" (Dec 6th 2004, 5:12pm)


boyz5

Unregistered

3

Monday, December 6th 2004, 5:17pm

well, but what am i supposed to do?

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

4

Monday, December 6th 2004, 5:23pm

Quoted

well, but what am i supposed to do?


Follow the instructions in the quote in my previous reply:

Quoted

use the boxLayout() function to get a pointer to the dock window's box layout, then add widgets to the layout using the box layout's QBoxLayout::addWidget() function.


(QDockWindow is the parent of QToolBar, so you can do all this to your toolbar.)

Good luck!

Sam

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Monday, December 6th 2004, 5:25pm

RE: QComboBox in QToolbar ???

Source code

1
QComboBox *cb = new QComboBox(MyToolbar);


will be sufficient. You can then access cb as a normal combo box, connect signals to its slots and slots to its signals.

boyz5

Unregistered

6

Monday, December 6th 2004, 5:39pm

wysota you were right! it worked. but it is at the beginning of my toolbar, how can I put it at the end or on my specific place i want it to be?

boyz5

Unregistered

7

Monday, December 6th 2004, 5:56pm

uhh and by the way - how to make bold or italic texts in the combobox?
i tried:
combobox->insertItem("<b>Bold</b>", 15);

but its not working...

axeljaeger

Professional

  • "axeljaeger" is male

Posts: 730

Location: Frankfurt Germany

Occupation: Student

  • Send private message

8

Monday, December 6th 2004, 6:09pm

It's nice to be important but it's more important to be nice

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

9

Monday, December 6th 2004, 6:18pm

Quoted

how to make bold or italic texts in the combobox?


I haven't ever done this, but I think you'll need to create a QListBox subclass (using a QListBoxItem subclass that supports rich text) then use QComboBox::setListBox(...) to set the list box for the combo to your subclass. Phew...

There may well be a simpler way.

Sam

boyz5

Unregistered

10

Monday, December 6th 2004, 7:09pm

okay... i fixed the first problem but not i still cannot make some items in ComboBox bold or italic or so.
@samdutton - i guess there's an easier way to fix this :D

boyz5

Unregistered

11

Tuesday, December 7th 2004, 3:54pm

please! i need to know how i can realize that! i'd appreciate every help i could get!

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

12

Tuesday, December 7th 2004, 4:03pm

Did you try what I suggested?

Quoted

create a QListBox subclass (using a QListBoxItem subclass that supports rich text) then use QComboBox::setListBox(...) to set the list box for the combo to your subclass.

It looks a bit complex, but shouldn't be that difficult.

Sam

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

13

Tuesday, December 7th 2004, 4:06pm

QComboBox doesn't offer rich text by itself. You have to use some workaround or subclass QComboBox and change its method responsible for drawing the text (you can use QSimpleRichText to ease your life).

boyz5

Unregistered

14

Tuesday, December 7th 2004, 4:29pm

i guess the advice of samdutton is the better one ;)

void MyListBoxItem::paint(QPainter *p)
{
QListBoxItem::paint(p);
//....
}

The compiler gives me this error:

test.obj : error LNK2019: unresolved external symbol "protected: virtual void __thiscall QListBoxItem::paint(class QPainter *)" (?paint@QListBoxItem@@MAEXPAVQPainter@@@Z) referenced in function "private: virtual void __thiscall MyListBoxItem::paint(class QPainter *)" (?paint@MyListBoxItem@@EAEXPAVQPainter@@@Z)
.\test.exe : fatal error LNK1120: 1 unresolved externals

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

15

Tuesday, December 7th 2004, 4:50pm

Did you have #include <qlistbox.h> in your header? You might also need a forward declaration: class QListBoxItem;

Sam

boyz5

Unregistered

16

Tuesday, December 7th 2004, 5:06pm

test.h
-----------
#include<qlistbox.h>

class MyListBoxItem : public QListBoxItem
{
void paint(QPainter *);
};

test.cpp
------------
#include "test.h"

void MyListBoxItem::paint(QPainter *p)
{
QListBoxItem::paint(p);
}

that's it (kinda)!

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

17

Tuesday, December 7th 2004, 5:08pm

Looks like you forgot

Source code

1
2
3
4
5
6
7
8
9
Q_OBJECT

public: 

...

private:

...

at the top of the class declaration.

Sam

This post has been edited 1 times, last edit by "samdutton" (Dec 7th 2004, 5:09pm)


boyz5

Unregistered

18

Tuesday, December 7th 2004, 5:13pm

well... if i do that:

class MyListBoxItem : public QListBoxItem
{
Q_OBJECT
public:
MyListBoxItem(QListBox *listbox=0);
private:
void paint(QPainter *);
};

then i got even more errors:

tmp\moc\release_mt_static\moc_test.cpp(336) : error C2039: 'staticMetaObject' : is not a member of 'QListBoxItem'
C:\Qt\include\qlistbox.h(316) : see declaration of 'QListBoxItem'
tmp\moc\release_mt_static\moc_test.cpp(354) : error C2039: 'qt_cast' : is not a member of 'QListBoxItem'
C:\Qt\include\qlistbox.h(316) : see declaration of 'QListBoxItem'
tmp\moc\release_mt_static\moc_test.cpp(359) : error C2039: 'qt_invoke' : is not a member of 'QListBoxItem'
C:\Qt\include\qlistbox.h(316) : see declaration of 'QListBoxItem'
tmp\moc\release_mt_static\moc_test.cpp(364) : error C2039: 'qt_emit' : is not a member of 'QListBoxItem'
C:\Qt\include\qlistbox.h(316) : see declaration of 'QListBoxItem'
tmp\moc\release_mt_static\moc_test.cpp(370) : error C2039: 'qt_property' : is not a member of 'QListBoxItem'
C:\Qt\include\qlistbox.h(316) : see declaration of 'QListBoxItem'

samdutton

Intermediate

  • "samdutton" is male

Posts: 160

Location: Australian in London

Occupation: C++ and Web developer

  • Send private message

19

Tuesday, December 7th 2004, 5:15pm

Oops -- leave out the Q_OBJECT.

QListBoxItem doesn't inherit QObject.

Sam

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

20

Tuesday, December 7th 2004, 8:18pm

You can't use QListBoxItem::paint(...) as it is a pure abstract method (it doesn't exist there, look that a null is assigned to it).