简体   繁体   中英

Not able to connect to the slot in qt

Here i am adding part of my code, i have my main function and MainWindow class

Basically my init() function should be triggered in order to fill my display_images queue in timercalldisplay() function so i am calling it from my main

But i am not able to get in to timercalldisplay() calling it through SLOT and init() is getting executed

Can anybody say what mistake i was doing

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void init();

    QTimer *timer;

public slots:
    void timercalldisplay();

private:
    Ui::MainWindow *ui;

};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.init();

    return a.exec();
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timercalldisplay()));
    timer->start(500);

}

void MainWindow::timercalldisplay()
{
    qDebug() << display_images.size();                        //not reaching here
    while(display_images.size())
    {
        Mat img = display_images.front();
        QPixmap pixmap;
        QImage qimg(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
        pixmap = QPixmap::fromImage(qimg.rgbSwapped());
        ui->label->setPixmap(pixmap);
        ui->label->setScaledContents(true);
        ui->label->show();
        QThread::currentThread()->msleep(1000);
        qApp->processEvents();
        display_images.pop();
    }
}
connect(timer, SIGNAL(timeout()), this, SLOT(timercalldisplay()));

take the () out of the connect like fixed

connect(timer, &QTimer::timeout, this, &MainWindow::timercalldisplay);

Also in you constructor change

QTimer *timer = new QTimer(this);

to

timer = new QTimer(this);

Update based on new issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM