Not only that, but slots and signals can't return values (i.e. they are declared as void someFunc()). If you need something passed or returned through a signal / slot mechanism, pass an argument:
// counter.h
signals:
void countIncremented( int newCount );
public slots:
void count();
// counter.cpp
void counter::count()
{
number += 10;
emit countIncremented( number );
qDebug() << number;
}
// MainWindow.cpp
// In the constructor:
counter * counting = new counter( this );
connect(ui->pushButton_2,&QPushButton::clicked, counting, &counter::count );
connect( counting, &counter::countIncremented(), ui->progressBar, &QProgressBar::setValue() );
// counter.h
signals:
void countIncremented( int newCount );
public slots:
void count();
// counter.cpp
void counter::count()
{
number += 10;
emit countIncremented( number );
qDebug() << number;
}
// MainWindow.cpp
// In the constructor:
counter * counting = new counter( this );
connect(ui->pushButton_2,&QPushButton::clicked, counting, &counter::count );
connect( counting, &counter::countIncremented(), ui->progressBar, &QProgressBar::setValue() );
To copy to clipboard, switch view to plain text mode
The other problem is that by declaring counter::number as static, all instances of the counter object will be using the same value. So if you have two push buttons, even if they are connected to different instances of the counter object they will be incrementing the same value. If the value is initialized to zero, on the first click it becomes 10, the second click 20, and so on, regardless of which button gets clicked.
And further, if different instances of push buttons are connected to different instances of counters, and the counters are connected to different instances of progress bars, then the progress bars will be out of sync with the counter value because they are not connected to the same counter instance.
Button1 -> counter 1 -> progress bar 1
Button2 -> counter 2 -> progress bar 2
- Start: Number = 0, progress bar values = 0
- Click button 1
- Counter 1 increments; number = 10
- Progress bar 1 increments; value = 10
- Progress bar 2 doesn't increment; value = 0
- Click button 2
- Counter 2 increments; number = 20
- Progress bar 2 increments; value = 20
- Progress bar 1 doesn't increment; value = 10
Is that what you intend to happen? If not, don't make "number" a static member of counter.
Bookmarks