Solved QObject::connect: No such slot MainWindow::getThing(ctx)
-
I am trying to create a GUI that interfaces with a redis server to get key values and display them in the GUI. When I build and run the code, I get an error:
QObject::connect: No such slot MainWindow::getThing(ctx).After looking at the stack overflow answers, I still don't understand why my GUI can't recognize the slot that I created.
Main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
MainWindow.cpp
include "mainwindow.h" #include "ui_mainwindow.h" #include "hiredis/hiredis.h" #include <string> #include <iostream> #include <QString> #include <QTimer> #include <QVector> #include <QListWidget> #include <QDateTime> #include <QFileDialog> #include <pthread.h> #include <QtConcurrent> QString getRedisValue(redisContext *ctx, QString cmdPrefix, QString keyName); void getThing(redisContext *ctx); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle(tr("Vorrina")); //Establish redis connection redisContext *ctx = redisConnect("127.0.0.1", 6379); if (ctx != NULL && ctx->err){ printf("Error: %s\n", ctx->errstr); // handle error } //ui->label->setText("Hello"); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(getThing(ctx))); timer->start(1000); } MainWindow::~MainWindow() { delete ui; } QString MainWindow::getRedisValue(redisContext *ctx, QString cmdPrefix, QString keyName){ QString cmd= cmdPrefix + keyName; QByteArray cmdBytes = cmd.toLocal8Bit(); redisReply *reply; void *pointer = NULL; pointer = redisCommand(ctx, cmdBytes.constData()); reply = (redisReply*)pointer; QString keyValue = QString(reply->str); freeReplyObject(reply); return keyValue; } void MainWindow::getThing(redisContext *ctx){ QString label = getRedisValue(ctx, "GET ", "answer"); if(QString::compare(label, "ITS_HERE") == 0){ ui->label->setText("ITS HERE"); ui->label->setStyleSheet("QLabel { background-color : red; }"); } else if(QString::compare(label, "NOT_HERE") == 0){ ui->label->setText("NOT HERE"); ui->label->setStyleSheet("QLabel { background-color : #90ee90; }"); } else{ ui->label->setText("LOOKING FOR IT"); ui->label->setStyleSheet("QLabel { background-color : yellow; }"); } }
Mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <string.h> #include <hiredis/hiredis.h> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QString getRedisValue(redisContext *ctx, QString cmdPrefix, QString keyName); public slots: void getThing(redisContext *ctx); private: Ui::MainWindow *ui; QTimer *timer; }; #endif // MAINWINDOW_H
-
Hi,
You can't pass parameters values in your connect calls. Even more you can't do a connection from a signal with no parameters with a slot that has parameters unless they have all default values.
-
Hi,
You can't pass parameters values in your connect calls. Even more you can't do a connection from a signal with no parameters with a slot that has parameters unless they have all default values.