You are not logged in.

1

Sunday, September 5th 2004, 4:31pm

A quustion about toolbar....

It's another big question to anda_skoa, - I think so ;).

I read your answer how to create a toolbar in various widgets not in QMainWindow only. I use a toolbar in dialog window. My dialog is like the Open dialog in Microsoft Word.

I need to create combined button. I knew it's button would be created using QActionGroup class. My problem is how can I use only text for my button I don't need an image. I knew that I have to use setUsesTextLabel(true) for QMainWindow. But my main window is QDialog. And if I try to convert a pointer to my dialog to QMainWindow using <dynamic_cast> I couldn't see a text on the button. What can I do to see a text on my QActionGroup button?

dimitri

Professional

  • "dimitri" is male

Posts: 1,311

Occupation: Engineer

  • Send private message

2

Sunday, September 5th 2004, 4:44pm

RE: A quustion about toolbar....

Casting a widget that is not QMainWindow to a QMainWindow is guaranteed to crash your program. Indeed dynamic_cast<> will return 0 in that case.

Otherwise have a look at the source code for QMainWindow::setUsesTextLabel(). It's really simple:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*!
    \property QMainWindow::usesTextLabel
    \brief whether text labels for toolbar buttons are enabled

    If disabled (the default), the tool buttons will not use text
    labels. If enabled, text labels will be used.

    Tool buttons and other widgets that wish to respond to this
    setting are responsible for reading the correct state on startup,
    and for connecting to the main window's widget's
    usesTextLabelChanged() signal.

    \sa QToolButton::setUsesTextLabel()
*/

bool QMainWindow::usesTextLabel() const
{
    return d->utl;
}


void QMainWindow::setUsesTextLabel( bool enable )
{
    if ( enable == (bool)d->utl )
	return;

    d->utl = enable;
    emit usesTextLabelChanged( enable );

    QObjectList *l = queryList( "QLayout" );
    if ( !l || !l->first() ) {
	delete l;
	return;
    }
    for ( QLayout *lay = (QLayout*)l->first(); lay; lay = (QLayout*)l->next() )
	    lay->activate();
    delete l;
}
As you can see this merely sets a property, and QToolButtons react to it. QTooButtons have a similar function:
QToolButton::setUsesTextLabel

3

Monday, September 6th 2004, 9:04am

RE: A question about toolbar....

I need a button like "Back" & "Forward" in MS Internet Explorer or like button "New" in demo.exe from QT examples (3D Graphics->3D Demo and see a toolbar). I think I can create my own button such as described above only using a class QActionGroup. I couldn't see how can I use a class QToolButtons to create this button if U know how to do it using a class QToolButtons, please, give me a piece of your code. My problem is to use a text for the button but not the image.

The function setUsesTextLabel is a part of QToolButton and QMainWindow classes. I ask how can I do what I need ?( ?

This is a piece of my code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
ISFileDialog::ISFileDialog()
      : QDialog(qApp->activeWindow(), "dialog", true, WStyle_Customize | WType_TopLevel | WStyle_NormalBorder 
		  | WStyle_Title | WStyle_SysMenu | WShowModal)
{
	// Create Widgets which allow easy layouting
	QVBoxLayout *pVBox = new QVBoxLayout(this, 10, 5);

                     ......

	QHBoxLayout *pHBox3 = new QHBoxLayout(pVBox, 0);

                     ......
	
	QToolBar *pToolbar2 = new QToolBar(0, 0, this);
	pToolbar2->setFixedExtentWidth(50);
	pToolbar2->setFixedExtentHeight(24);
	pToolbar2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	
	m_pOpenGroup = new QActionGroup(pToolbar1);
    
	m_pOpenGroup->setMenuText(tr("&Open"));
                     m_pOpenGroup->setText(tr("Open"));
                     m_pOpenGroup->setUsesDropDown(true);
                     m_pOpenGroup->setExclusive(false);
	
	m_pOpenAction = new QAction(tr("&Open"), 0, m_pOpenGroup);
	m_pOpenAsCopyAction = new QAction(tr("O&pen as copy"), 0, m_pOpenGroup);
	m_pOpenReadOnlyAction = new QAction(tr("Op&en read only"), 0, m_pOpenGroup);
	
                      m_pOpenGroup->addTo(pToolbar2);
	pHBox3->add(pToolbar2);
                     ......
}

4

Monday, September 6th 2004, 3:14pm

Didn't anyone could help me with my trouble :rolleyes:?

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

5

Monday, September 6th 2004, 5:09pm

Can't you just use setText() method? Because I don't really see the problem here... You want to have a text on your QAction, so use its setText() method to apply such text...

6

Tuesday, September 7th 2004, 6:44am

I use QActionGroup class not QAction class.

And I use setText function, see:

Source code

1
m_pOpenGroup->setText(tr("Open"));


And I use setText function for each QAction class instance of this QActionGroup.

But If U see the documentation for QActionGroup class attentively U could see the next note:

Non-exclusive groups are represented by a tool button showing their QAction::iconSet and -- depending on QMainWindow::usesTextLabel() -- text() property.

My code described above is lain into a DLL.
But I have QMainWindow, I create a toolbar in a QDialog class instance. Therein lies my problem.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

7

Tuesday, September 7th 2004, 8:43am

QActionGroup is a descendant of QAction...

You could do two things:

1. Subclass QActionGroup and redefine the function that checks if it should display the text to make it always display it without even checking.

2. Use QActions instead of QActionGroup

3. Not to use QDialog but QMainWindow instead :)