Yes, it is, as I said earlier:
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
Five easy steps, and I even posted code that does it.
Here's more. Try it. Make a new project, one that does not contain anything to do with btscanner. Implement a MainWindow class with the slot, implement a class derived from QWidget with the signal, add a pushbutton to it, and make it the central widget of the QMainWindow. When you create the widget, connect the widget signal to the MainWindow slot. Add a slot to the widget to handle the clicked() signal from the button and every time you click the button, emit the signal, exactly like this:
// SignalWidget.h - no cpp file needed
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
class SignalWidget
: public QWidget{
Q_OBJECT
public:
{
pLayout->addWidget( pButton );
setLayout( pLayout );
connect( pButton, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
}
signals:
void sendStatus
( const QString & message
);
protected slots:
void buttonClicked()
{
static int numClicks = 0;
QString message
= QString( "Button clicked %1 times" ),arg
( ++numClicks
);
emit sendStatus( message );
}
};
// SignalWidget.h - no cpp file needed
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
class SignalWidget : public QWidget
{
Q_OBJECT
public:
SignalWidget( QWidget * parent = nullptr ) : QWidget( parent )
{
QVBoxLayout * pLayout = new QVBoxLayout( this );
QPushButton * pButton = new QPushButton( "Push Me!", this );
pLayout->addWidget( pButton );
setLayout( pLayout );
connect( pButton, SIGNAL( clicked() ), this, SLOT( buttonClicked() ) );
}
signals:
void sendStatus( const QString & message );
protected slots:
void buttonClicked()
{
static int numClicks = 0;
QString message = QString( "Button clicked %1 times" ),arg( ++numClicks );
emit sendStatus( message );
}
};
To copy to clipboard, switch view to plain text mode
// MainWindow.h -- no cpp file needed
#include <QMainWindow>
#include <QStatusBar>
#include "SignalWidget.h"
{
Q_OBJECT
public:
{
SignalWidget * pWidget = new SignalWidget( this );
connect( pWidget,
SIGNAL( sendStatus
( const QString & ) ),
this,
SLOT( displayStatusMessage
( const QString & ) ) );
setCentralWidget( pWidget );
}
protected slots:
void displayStatusMessage
( const QString & message
) {
pBar->showMessage( message, 5000 );
}
};
// MainWindow.h -- no cpp file needed
#include <QMainWindow>
#include <QStatusBar>
#include "SignalWidget.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow( QWidget * parent = nullptr ) : QMainWindow( parent )
{
SignalWidget * pWidget = new SignalWidget( this );
connect( pWidget, SIGNAL( sendStatus( const QString & ) ), this, SLOT( displayStatusMessage( const QString & ) ) );
setCentralWidget( pWidget );
}
protected slots:
void displayStatusMessage( const QString & message )
{
QStatusBar * pBar = statusBar();
pBar->showMessage( message, 5000 );
}
};
To copy to clipboard, switch view to plain text mode
// main.cpp
#include <QApplication>
#include "MainWindow.h"
int main( int argc, char * argv[] )
{
MainWindow mw;
mw.show();
return a.exec()
}
// main.cpp
#include <QApplication>
#include "MainWindow.h"
int main( int argc, char * argv[] )
{
QApplication a( argc, argv );
MainWindow mw;
mw.show();
return a.exec()
}
To copy to clipboard, switch view to plain text mode
I don't guarantee that there are no typos. But it will work.
To keep it simple, this example has no ui files (all the UI is done in code) and no cpp files except for main.cpp. You do have to make sure that MOC runs on the .h files, otherwise there will be no boilerplate code to implement the signals and slots.
Bookmarks