You are not logged in.
Pages: 1
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
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
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
Wow; that was much easier than I'd anticipated. Thanks!
Last edited by tony5429 (2009-06-19 16:53:45)
Offline
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
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
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
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
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
Pages: 1