I want to convert a QString from another class toInt, but it seams to fail because the result is 0 while it should be 5 and the rest of the code works.
Because I am new to C/Qt and find classes a bit confusing, I think I have done something wrong with my classes.
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
MyClass tmp;
int myNumber = tmp.m_replyStr.toInt();
ui->lcdNumber->display(myNumber);
}
MyClass::MyClass()
{
m_manager = new QNetworkAccessManager(this);
connect( m_manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
void MyClass::fetch()
{
m_manager->get(QNetworkRequest(QUrl("http://example.tld/test.html"))); // html contains only the number 5
}
void MyClass::replyFinished(QNetworkReply* pReply)
{
QByteArray data=pReply->readAll();
QString str(data);
m_replyStr = str;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QObject>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
};
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass();
void fetch();
QString m_replyStr;
public slots:
void replyFinished(QNetworkReply*);
private:
QNetworkAccessManager* m_manager;
};
#endif // WIDGET_H
You're accessing m_replyStr of a newly initialised instance tmp, which doesn't set anything into its m_replyStr. So it has the default-initialised value of "empty string."
EDIT
Based on your follow-up question, perhaps you were looking for something like this?
class MyClass;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(MyClass &myClassInstance, QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
MyClass *myClass;
};
Widget::Widget(MyClass &myClassInstance, QWidget *parent = 0)
: QWidget(parent)
, ui(new Ui::Widget)
, myClass(&myClassInstance)
{
ui->setupUi(this);
}
void Widget::on_pushButton_clicked()
{
int myNumber = myClass->m_replyStr.toInt();
ui->lcdNumber->display(myNumber);
}
Related
I know this question asked many times but still confused and cannot find a solution.
I have a MainWindow and a class.
In my class I have a signal which I emit in the method call.
Problem: Slot method is not firing.
Here is my code.
BWorker.h
#pragma once
#include <QObject>
class BWorker : public QObject
{
Q_OBJECT
public:
BWorker(QObject *parent);
~BWorker();
void doSomething();
signals:
void signalSomething();
};
BWorker.cpp
#include "BWorker.h"
BWorker::BWorker(QObject *parent)
: QObject(parent)
{
}
BWorker::~BWorker()
{
}
void BWorker::doSomething()
{
emit signalSomething();
}
QtGuiApplication1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
public slots:
void workDone();
private:
Ui::QtGuiApplication1Class ui;
};
QtGuiApplication1.cpp
#include "QtGuiApplication1.h"
#include "BWorker.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
BWorker bworker(this);
connect(&bworker, SIGNAL(bworker.signalSomething), this, SLOT(workDone()));
bworker.doSomething();
}
void QtGuiApplication1::workDone() {
}
Your problem is here:
SIGNAL(bworker.signalSomething)
This is not valid and you should see a message in terminal saying that there is no such signal. The correct syntax for your case would be:
SIGNAL(signalSomething())
Please post your code by copy-pasting it. Do not edit the code: The problem could be emerged from where you edited.
The code of mine worked fine.
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();
public slots:
void workDone();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "worker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Worker worker(this);
connect(&worker, SIGNAL(signalSomething()), this, SLOT(workDone()));
//connect(&worker, &Worker::signalSomething, this, &MainWindow::workDone);
worker.doSomething();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::workDone()
{
qDebug() << "Done.";
}
worker.h:
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void doSomething();
signals:
void signalSomething();
public slots:
};
#endif // WORKER_H
worker.cpp:
#include "worker.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::doSomething()
{
emit signalSomething();
}
Application Output:
Debugging starts
Done.
Debugging has finished
Printed out "Done." means the codes are working.
I want to get the currentIndex of combobox in the main Widget, then send this value to another class. I tried to make a function to return the currentIndex, however, I always get '0'. Could someone explain me why, or give me an example? Plus, I think it's because I create a new object in another class, but how can I get the actived Widget's pointer? Here are the codes:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui { class Widget; }
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
int comboboxText();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui; };
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include "database.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
int Widget::comboboxText(){
return ui->comboBox->currentIndex();
}
void Widget::on_pushButton_clicked()
{
qDebug() << "from Widget: " << comboboxText();
Database database;
database.getIndex();
}
database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <QObject>
#include <QDebug>
class Database : public QObject
{
Q_OBJECT
public:
explicit Database(QObject *parent = 0);
void getIndex();
signals:
public slots:
};
#endif // DATABASE_H
database.cpp
#include "database.h"
#include <widget.h>
Database::Database(QObject *parent) : QObject(parent)
{
}
void Database::getIndex(){
Widget a;
qDebug()<< "from database: " << a.comboboxText();
}
I cannot access staticmetaobject and I dont know why. I would need some help.
Here is the code
The two errors are:
staticMetaObject is not a member of MainWIndow*
I feel like it has something to do with the list, but I'm not sure.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "form.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Form<MainWindow*>* form;
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/*qDebug() << MainWindow::staticMetaObject.className();
if (QString(MainWindow::staticMetaObject.className()) == QString("MainWindow")) {
qDebug() << "test";
}*/
form = new Form<MainWindow*>(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
form->myFunc();
}
form.h
#ifndef FORM_H
#define FORM_H
#include <QObject>
#include <QDebug>
class FormBase : public QObject
{
Q_OBJECT
public:
FormBase() {}
};
template <typename T>
class Form : public FormBase, public QList<T>
{
public:
Form(T a)
{
QList<T>::append(a);
}
void myFunc()
{
qDebug() << T::staticMetaObject.className();
}
};
#endif // FORM_H
You are getting you types confused.
You want T to be MainWindow so that you can do
T::staticMetaObject.className()
That means you want a QList<T*>. You derive from that so you can just call
append(a);
The following code compiles fine:
class FormBase : public QObject
{
Q_OBJECT
public:
FormBase() {}
};
template <typename T>
class Form : public FormBase, public QList<T*>
{
public:
Form( T* a )
{
append( a );
}
void myFunc()
{
qDebug() << T::staticMetaObject.className();
}
};
class MainWindow:
public QMainWindow
{
MainWindow()
{
form = new Form<MainWindow>( this );
}
FormBase* form;
};
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemModel>
#include <QThread>
#include <statusdialog.h>
#include <pbt.h>
#include <stdint.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
//void on_treeView_clicked(const QModelIndex &index);
void onCustomContextMenuTV(const QPoint &point);
void dirSize();
void getSelectedTreeItemSize();
void resultHandle(uint64_t);
signals:
void sizeCalculation(uint64_t);
private:
Ui::MainWindow *ui;
QString sPath;
QFileSystemModel *dirmodel;
QFileSystemModel *filemodel;
QAction *dirSizeAct;
statusDialog statusdialog;
};
#endif // MAINWINDOW_H
pbt.h
#ifndef STATUSDIALOG_H
#define STATUSDIALOG_H
#include <QDialog>
#include <stdint.h>
namespace Ui {
class statusDialog;
}
class statusDialog : public QDialog
{
Q_OBJECT
public:
explicit statusDialog(QWidget *parent = 0);
~statusDialog();
void setProgressbarMax(uint64_t);
private slots:
void on_pushButton_clicked();
private:
Ui::statusDialog *ui;
};
#endif // STATUSDIALOG_H
statusdialog.h
#ifndef STATUSDIALOG_H
#define STATUSDIALOG_H
#include <QDialog>
#include <stdint.h>
namespace Ui {
class statusDialog;
}
class statusDialog : public QDialog
{
Q_OBJECT
public:
explicit statusDialog(QWidget *parent = 0);
~statusDialog();
void setProgressbarMax(uint64_t);
private slots:
void on_pushButton_clicked();
private:
Ui::statusDialog *ui;
};
#endif // STATUSDIALOG_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sPath = "C:/";
dirmodel = new QFileSystemModel(this);
dirmodel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
dirmodel->setRootPath(sPath);
ui->treeView->setModel(dirmodel);
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenuTV(const QPoint &)));
connect(this,SIGNAL(sizeCalculation(uint64_t)),this,SLOT(resultHandle(uint64_t)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resultHandle(uint64_t t_size)
{
statusdialog.setProgressbarMax(t_size);
}
void MainWindow::onCustomContextMenuTV(const QPoint &point)
{
dirSizeAct = new QAction(tr("Size"), this);
connect(dirSizeAct, SIGNAL(triggered()), this, SLOT(dirSize()));
QMenu contextMenu(this);
contextMenu.addAction(dirSizeAct);
QModelIndex index = ui->treeView->indexAt(point);
if (index.isValid()){
contextMenu.exec(ui->treeView->mapToGlobal(point));
}
}
void MainWindow::dirSize()
{
pBT pbt;
pbt.setFP(this->getSelectedTreeItemSize);
QThread thread1;
connect(&thread1,SIGNAL(started()),&pbt,SLOT(startThreadAction()));//Clone the object and it will not work, becouse it is QWidget
pbt.moveToThread(&thread1);
thread1.start();//Starts in the same thread
statusdialog.setModal(true);
statusdialog.exec();
}
void MainWindow::getSelectedTreeItemSize()
{
QModelIndex index = ui->treeView->selectionModel()->selectedIndexes().takeFirst();
QString dirPath = dirmodel->filePath(index);
QDirIterator it(dirPath, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories);
uint64_t t_size = 0;
while (it.hasNext()) {
QFile myFile(it.next());
if (myFile.open(QIODevice::ReadOnly)){
t_size += myFile.size();
myFile.close();
}
}
emit sizeCalculation(t_size);
}
pbt.cpp
#include "pbt.h"
pBT::pBT(QObject *parent) : QObject(parent)
{
}
void pBT::startThreadAction()
{
this->fp1();
}
void pBT::setFP(void (*fp1)())
{
this->fp1 = fp1;
}
statusdialog.cpp
#include "statusdialog.h"
#include "ui_statusdialog.h"
statusDialog::statusDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::statusDialog)
{
ui->setupUi(this);
}
statusDialog::~statusDialog()
{
delete ui;
}
void statusDialog::on_pushButton_clicked()
{
this->close();
}
void statusDialog::setProgressbarMax(uint64_t size)
{
ui->progressBar->setMaximum(size);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To see more info of what I am doing you can look this topic(It is all about prograssbar and threaded execution of job). Theoretically I whant to run a member function of mainwindow(which use member variables of mainwindow) into new thread(the function is not static) inside slot dirSize()(also member slot of the same class mainwindow), but it seems with QThread it is necessary to create new class(no matter if I will inherit QThread class or use moveToThread function of QObject) and run that class in new thread. If I use C++ thread.h the function I run must be static, nevermind I have tried to create pBT class in which I have function pointer fp1 and public slot startThreadedAction, but when try to buil this error occures:
C:\Users\niki\Documents\EPsimple\mainwindow.cpp:54: error: C3867:
'MainWindow::getSelectedTreeItemSize': function call missing argument
list; use '&MainWindow::getSelectedTreeItemSize' to create a pointer
to member
I don`t want to create function pointer to static function! How can I fix this?
I have made made a DDisplay class to display my data on gui text box but I have some strange situation, when I call the function from mainwindow class it is working fine but when I try to call the same function from other class it is not working. below is the small code my program
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <ddisplay.h>
#include <QMainWindow>
#include "test.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
DDisplay b;
Test c;
private slots:
void display(const QString &a);
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(&b,SIGNAL(display(QString)),this,SLOT(display(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::display(const QString &a){
ui->textBrowser->setText(a);
}
void MainWindow::on_pushButton_clicked()
{
b.printf("hello"); //this function working fine
//c.printf("lllll"); //not working
}
test.h
#ifndef TEST_H
#define TEST_H
#include <QString>
#include <ddisplay.h>
class Test
{
public:
Test();
void printf(const QString &a);
DDisplay b;
};
test.cpp
#include "test.h"
Test::Test()
{
}
void Test::printf(const QString &a){
b.printf(a);
}
ddisplay.h
#include
class DDisplay : public QObject
{
Q_OBJECT
public:
explicit DDisplay(QObject *parent = 0);
void printf(const QString &a);
QString b;
signals:
void display(const QString &a);
public slots:
};
ddisplay.cpp
#include "ddisplay.h"
DDisplay::DDisplay(QObject *parent) :
QObject(parent)
{
}
void DDisplay::printf(const QString &a)
{
b+=a;
emit display(b);
}
c.printf("lllll"); //not working
It's not working because you don't have a connect for this object. DDisplay in Test class is a dfferent instance than that in a MainWindow.
One possible solution for your question in comments below is to connect DDisplay's signal and MainWindow's slot either in main.cpp or in specialized initialization class.