You are not logged in.

#1 2009-06-18 21:24:04

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

[SOLVED] How to Use a QStatusBar

I'm working on a small Qt project and would like to use the QStatusBar in my QMainWindow. I've added "QStatusBar *statusBar;" to the private section of "class MainWindow : public QMainWindow" in my mainwindow.h, but when I try to add "setStatusBar(statusBar);" to the "MainWindow::MainWindow()" section of my mainwindow.cpp, it compiles fine and then segfaults when I try to run it. Does anyone have any ideas? Thanks.

Last edited by tony5429 (2009-06-21 00:58:22)

Offline

#2 2009-06-18 21:40:17

sirius
Member
From: Norway
Registered: 2008-12-25
Posts: 68

Re: [SOLVED] How to Use a QStatusBar

Have you checked out docs and examples?

http://doc.trolltech.com/4.5/qstatusbar.html#details
http://doc.trolltech.com/4.5/mainwindow … ation.html

I hope this can help you some way :-)

Offline

#3 2009-06-18 23:18:42

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] How to Use a QStatusBar

QStatusBar * QMainWindow::statusBar () const

Returns the status bar for the main window. This function creates and returns an empty status bar if the status bar does not exist.

See also setStatusBar().

You don't need to create a new one, just call window->statusBar() and set the attributes of it.

Offline

#4 2009-06-19 16:53:01

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Use a QStatusBar

Wow; that was much easier than I'd anticipated. Thanks!

Last edited by tony5429 (2009-06-19 16:53:45)

Offline

#5 2009-06-19 18:32:19

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Use a QStatusBar

Looks like I spoke too fast again. I realised the best way for me to do it would be to create a private QLabel member in mainwindow.h and connect it to the QStatusBar using addPermanentWidget().

Now I've got a public function in my "class MainWindow : public QMainWindow" section of "mainwindow.h" which updates the status bar. I'd like to call this public function whenever the user clicks a button in the main widget. However, because the main widget is a child? of the main window, I'm not sure what to replace XXXX with in the following code,

QObject::connect(item12, SIGNAL(clicked()), XXXX, SLOT(setTheStatus()));

Does anyone have any ideas?

Offline

#6 2009-06-20 00:08:14

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] How to Use a QStatusBar

XXXX would be the instance of a class that has the member setTheStatus().  If it is called from inside the main window code it would be "this" sans quotes.

Edit: code tags seemed excessive.

Last edited by scio (2009-06-20 00:08:48)

Offline

#7 2009-06-20 04:35:37

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Use a QStatusBar

Thanks; I know I'm missing something easy here. The MainWindow instance, simply called "mainWindow," is created in main.cpp so when I change XXXX to "mainWindow" (sans quotes), I get the error,

'mainWindow' was not declared in this scope

How can I let the QObject::connect() have access to mainWindow? Thanks.

Offline

#8 2009-06-20 14:31:55

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [SOLVED] How to Use a QStatusBar

You have to call connect from somewhere that has access to item12 as well as mainWindow.  I would think this would be inside the MainWindow class.  If that is true, inside the constructor of MainWindow call:

connect(item12, SIGNAL(clicked()), SLOT(setTheStatus()));

or, to be explicit:

connect(item12, SIGNAL(clicked()), this, SLOT(setTheStatus()));

If you have this code hosted somewhere, it might be easier to point to the code with these problems instead of just posting sections.

You also might want to move these types of questions to "General Programming Forum", I just realized they are currently in the Newbie Corner.

Offline

#9 2009-06-21 01:10:02

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: [SOLVED] How to Use a QStatusBar

Thanks for the help. Unfortunately I could not get your solution to work. However, thankfully, I was able to figure out a crude work-around to solve the problem.

I'm still planning to release the code this month, so you'll see it in full soon. Thanks for all your help!

Offline

Board footer

Powered by FluxBB