Qt: QObject::connect: Cannot connect (null) - qt

I'm trying to connect a signal from a QProcess inside my mainwindow() object to another QObject based class inside my mainwindow() object but I get this error:
QObject::connect: Cannot connect (null)::readyReadStandardOutput () to (null)::logReady()
Heres the code, its not complete by any means but I don't know why it doesn't work.
exeProcess.h
#ifndef EXEPROCESS_H
#define EXEPROCESS_H
#include <QObject>
class exeProcess : public QObject
{
Q_OBJECT
public:
explicit exeProcess(QObject *parent = 0);
signals:
void outLog(QString outLogVar); //will eventually connect to QTextEdit
public slots:
void logReady();
};
#endif // EXEPROCESS_H
exeProcess.cpp
#include "exeprocess.h"
exeProcess::exeProcess(QObject *parent) :
QObject(parent)
{
}
void exeProcess::logReady(){
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include "exeprocess.h"
/*main window ---------------------------------------*/
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess *proc;
exeProcess *procLog;
public slots:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));
}
MainWindow::~MainWindow()
{
delete ui;
}
Thanks!.

You need to create the proc and procLog objects.
You've only got pointers as class members, so you'll have to initialize those (with new). connect only works on live objects.

proc is a pointer, but it does not point to anything. You have to instantiate a qprocess before you connect it!
proc = new QProcess();
connect(proc, SIGNAL(readyReadStandardOutput ()), procLog, SLOT(logReady()));

Related

Connect button to a function QT c++

I'm trying to connect a button to a function and after countless amounts of searches I couldn't find anything.
I could use a on_button_clicked(goto slot) function but I plan on re-assigning the button to run more code, so I decided to use the connect() class but when I run the application nothing happens when I type into the text box and click the game_user_input_submit button I have created, and im not sure if it is even calling the function.
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QtCore>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
user_name_set = false;
ui->game_chat_box->append("hi"); // a text edit box i created
if(user_name_set == false)
{
connect(ui->game_user_input_submit, SIGNAL(clicked()), SLOT(setUsername()));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setUsername()
{
username = ui->game_user_input->text(); // get text from line edit i created
user_name_set = true;
ui->game_chat_box->append("BOT: Welcome " + username);
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
void setUsername();
~MainWindow();
private slots:
//void on_game_user_input_submit_clicked();
private:
bool user_name_set = false;
QString username;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
oh and I'm kinda new to programming c++ in general so if anybody does give me some usefull information I might need it to explain it to me, sorry
connect should be of the form:
connect(object_pointer, signal_name, slot_name)
In your case, you are setting the slot name to setUsername() which is not a slot - it is just a public function.
Your mainwindow.h should be:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void setUsername();
private:
bool user_name_set = false;
QString username;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

Signal and Slot understanding in Qt

I am having problem with "Signal and Slot" understanding. Here below is my task description.
Select an item from Combo Box
Display that selected item, by a Label in the mainwindow.
It is very simple thing but I am not able to solve it.
Here below is my code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void activated(QString);
private slots:
void setLabelValue(const QString &text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->comboBox, &QComboBox::activated(QString),
this, &MainWindow::setLabelValue(QString));
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::setLabelValue(const QString &text)
{
//text = ui->comboBox->currentText();
text = static_cast<void (QComboBox::*)(QString)>(ui->comboBox->currentText());
ui->label->setText(text);
}
I hope you will forgive this novice's coding and understanding style.
The problem is you need to use the QLabel accessor function to change the text value. text() is a 'getter', not a 'setter'.
change MainWindow::setLabelValue() to
MainWindow::setLabelValue()
{
ui->label->setText(ui->comboBox->currentText());
}
Also, your connection isn't quite right. setLabelValue is a function of MainWindow, not the label, so change the connection to:
void (QComboBox::* activatedOverloadPtr)(const QString&) = &QComboBox::activated;
connect(ui->comboBox,activatedOverloadPointer, this, &MainWindow::setLabelValue);
Activated overload pointer is used so that the compiler can resolve which specific activated connection you are looking for.
The complete code would look something like:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//signals: // this is defined by QComboBox, not you (in this case)
// void activated(QString);
private slots:
void setLabelValue(const QString &text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
void (QComboBox::* activatedOverloadPtr)(const QString&) = &QComboBox::activated;
connect(ui->comboBox,activatedOverloadPointer, this, &MainWindow::setLabelValue);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::setLabelValue()
{
ui->label->setText(ui->comboBox->currentText());
}
Thanks all for your support. Here below the code, that has worked.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void setLabelValue(const QString &text);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated), this,
&MainWindow::setLabelValue);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setLabelValue(const QString &text)
{
ui->label->setText(text);
}

Need help connecting signal from thread to slot in GUI

mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void disableStartButton();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
myobject.h
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <QtCore>
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject *parent = 0);
void doSetup(QThread &cThread);
signals:
void disableStartButton();
public slots:
void doWork();
};
#endif // MYOBJECT_H
main.cpp:
#include "mainwindow.h"
#include "myobject.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QThread cThread;
MyObject cObject;
cObject.doSetup(cThread);
cObject.moveToThread(&cThread);
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::disableStartButton() {
ui->pushButton->setEnabled(false);
}
myobject.h:
#include "myobject.h"
#include <QDebug>
MyObject::MyObject(QObject *parent) :
QObject(parent)
{
}
void MyObject::doSetup(QThread &cThread)
{
connect(&cThread, SIGNAL(started()), this, SLOT(doWork()));
connect(ui->pushButton, SIGNAL(clicked()), &cThread, SLOT(start())); // (1)
connect(this, SIGNAL(disableStartButton()), ui, SLOT(disableStartButton())); // (2)
}
void MyObject::doWork()
{
emit disableStartButton();
qDebug() << "1";
}
I'm trying to do two things.
(1): As soon pushButton is clicked, I want cThread to start.
(2): As soon as cThread starts, it does: emit disableStartButton();. I want to connect disableStartButton() to disableStartButton() in mainwindow.cpp.
For (1), I get this error:
myobject.cpp:12: error: C2065: 'ui' : undeclared identifier
Of course, there ui is undefined. You try to connect ui->pushButton
connect(ui->pushButton, SIGNAL(clicked()), &cThread, SLOT(start())); // (1)
in file myobject.cpp, when your ui declared only in mainwindow.cpp
You have to connect pushbutton to your thread within mainwindow.cpp file, hope that helps
By the way, you should create your objects NOT in main.cpp but, in your case, in mainwindow.cpp, move lines
QThread cThread;
MyObject cObject;
cObject.doSetup(cThread);
cObject.moveToThread(&cThread);
to file mainwindow.cpp in constructor, for example
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
right after ui->setupUi(this);

Signal and Slot in Qt4 - not working

I am using Qt4 and Qt Creator. I cannot write a custom slot to a progress bar in the UI. How to write a custom slot for a specific widget in the ui file ? In my scenario, signal is not from the ui element.
the below code produces an error while running:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QFile>
#include<QFileInfo>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btcopy_clicked();
void on_btquit_clicked();
void ChangeValue(qint64 val);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btcopy_clicked()
{
QFileInfo *qfi=new QFileInfo("C:\\Users\\kiran\\Desktop\\test\\a.iso");
qDebug("%d" ,qfi->size());
QFile *qf=new QFile();
QFile fromFile("C:\\Users\\kiran\\Desktop\\test\\a.iso");
QFile toFile("C:\\Users\\kiran\\Desktop\\test\\b.iso");
ui->pbar->setMaximum(fromFile.size());
fromFile.copy(toFile.fileName());
connect(&toFile, SIGNAL(bytesWritten(qint64)), ui->pbar, SLOT(CangeValue(qint64)));
qDebug("completed");
}
void MainWindow::on_btquit_clicked()
{
exit(0);
}
void MainWindow::CangeValue(qint64 val)
{
ui->pbar->setValue(val);
}
Error Message
Object::connect: No such slot ProgressBar::CangeValue(qint64)in..\untitled\mainwindow.cpp:26
Object::connect: (receiver name: 'pbar')
CangeValue is a slot in your MainWindow (for the record: it should be called ChangeValue).
Therefore the third parameter in your connect(..) statement must be your main window, not the progress bar. Replace ui->pbar by this in your connect statement.

Qt readAllStandardOutput() causes unexpected program end

When I try to emit readAllStandardOutput() to a QString im getting an unexpected program crash, even if i connvert the QByteStream to a QString, any idea why that is? heres teh source
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include <QString>
#include "exeprocess.h"
/*main window ---------------------------------------*/
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QProcess *proc;
signals:
void outLog(QString outLogVar); //plug this into the QTextEdit box
public slots:
void logReady(); // plug the QProcess into this
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include <QByteArray>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QProcess *proc = new QProcess; //initialize proc
QStringList arguments;
arguments << "-h";
connect(proc, SIGNAL(readyReadStandardOutput ()), this, SLOT(logReady()));
proc->start("/Applications/Graphics/3Delight-9.0.87/bin/renderdl", arguments);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::logReady(){
QString str = proc->readAllStandardOutput();
emit outLog(str);
}
Thanks!
This line is the problem:
QProcess *proc = new QProcess; //initialize proc
You're shadowing the member variable by reusing that name. When logReady is called, the 'proc' that you call readAllStandardOutput() on is a different (null) pointer and so everything crashes. The fix is simple: replace the above line with
proc = new QProcess;

Resources