connect ( tab class , SIGNAL tab class signal "finshed", MainWindow class , SLOT set "statusBar (message )
Again, you need to read the Qt documentation on signals and slots so you can understand how they work. Look at the Qt examples to see how they are used in real code.
The C++ function signature for a slot must match the function signature for the signal. In other words, your slot can't be defined with an argument that does not exist in the signal (like your 'message' argument). If a class emits a signal with no arguments, the signal / slot mechanism can't just make something up to put in the argument defined for the slot it is connected to.
So if you want a widget in one of your tabs to send a string (message) back to your main window so the main window can display it in the status bar, then you need do do several things:
1 - define a signal in your widget with a suitable name (sendStatus maybe) and a QString argument: void sendStatus( const QString & message )
2 - define a slot in your MainWindow class with the same signature (void displayStatusMessage( const QString & message ))
3 - implement code in this slot to display the string argument (message) in the status bar
4 - when you create the widget for the tab, connect the widget's signal to the main window's slot.
5 - when your widget wants to display a status message, call "emit sendStatus( "Here is a message" );" Since that signal is now connected to the main window's slot, the slot will take care of displaying it
The main window's slot should look something like this:
// MainWindow.h
{
Q_OBJECT
// ...
protected slots:
void displayStatusMessage
( const QString & message
);
// ...
};
// MainWindow.cpp
// MainWindow constructor:
{
//...
MyWidget * pWidget = new MyWidget();
myTabWidget->addTab( myWidget, "My Widget" );
connect( pWidget,
SIGNAL( sendStatus
( const QString & ) ),
this,
SLOT( displayStatusMessage
( const QString & ) ) );
// ...
}
void MainWindow
::displayStatusMessage( const QString & message
) {
pStatusBar->showMessage( message, 5000 ); // A 5 second timeout
}
// MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
// ...
protected slots:
void displayStatusMessage( const QString & message );
// ...
};
// MainWindow.cpp
// MainWindow constructor:
{
//...
MyWidget * pWidget = new MyWidget();
myTabWidget->addTab( myWidget, "My Widget" );
connect( pWidget, SIGNAL( sendStatus( const QString & ) ), this, SLOT( displayStatusMessage( const QString & ) ) );
// ...
}
void MainWindow::displayStatusMessage( const QString & message )
{
QStatusBar * pStatusBar = statusBar();
pStatusBar->showMessage( message, 5000 ); // A 5 second timeout
}
To copy to clipboard, switch view to plain text mode
Alternatively, since QStatusBar::showMessage() is a slot itself, you could connect this slot directly to your widget's signal, which will cause it to display with a zero timeout. In this case, the message will be displayed until it is cleared or replaced by some other new message, either from your code or frfom a tooltip (which also uses the status bar):
// MainWindow constructor
MyWidget * pWidget = new MyWidget();
myTabWidget->addTab( myWidget, "My Widget" );
connect( pWidget,
SIGNAL( sendStatus
( const QString & ) ), pStatusBar,
SLOT( showMessage
( const QString & ) ) );
// MainWindow constructor
MyWidget * pWidget = new MyWidget();
myTabWidget->addTab( myWidget, "My Widget" );
QStatusBar * pStatusBar = statusBar();
connect( pWidget, SIGNAL( sendStatus( const QString & ) ), pStatusBar, SLOT( showMessage( const QString & ) ) );
To copy to clipboard, switch view to plain text mode
This works even though the slot has an extra argument which defaults to zero.
Remember, code for signals is automatically created by the MOC compiler. All you have to do is declare the signal in your widget class and that's it:
// MyWidget.h
{
Q_OBJECT
// ...
signals:
void sendStatus
( const QString & status
);
//...
};
// NOTHING goes in MyWidget.cpp to implement this signal.
// MyWidget.h
class MyWidget : public QWidget
{
Q_OBJECT
// ...
signals:
void sendStatus( const QString & status );
//...
};
// NOTHING goes in MyWidget.cpp to implement this signal.
To copy to clipboard, switch view to plain text mode
Any place in MyWidget where you want to update the status, you simply do this:
void MyWidget::SomeMethod()
{
emit sendStatus( "Status from SomeMethod" );
}
void MyWidget::SomeMethod()
{
emit sendStatus( "Status from SomeMethod" );
}
To copy to clipboard, switch view to plain text mode
Also remember that signals and slots both have void return values. You cannot return anything from a slot unless you pass an argument that is either a pointer or a reference that the slot can modify:
signals:
void someSignal( int & returnValue );
signals:
void someSignal( int & returnValue );
To copy to clipboard, switch view to plain text mode
public slots:
void someSlot( int & returnValue )
{
returnValue = 42;
}
public slots:
void someSlot( int & returnValue )
{
returnValue = 42;
}
To copy to clipboard, switch view to plain text mode
Just remember that if the same signal is connected to more than one slot, the -last- slot to handle the signal will wipe out any value set by previous slots.
Bookmarks