Qt passing a Value from one window to antother - c++

I am quite new to programming, so hopefully someone can help me out. After calculating a value with a function and storing it into the variable with a setter, I want to call it in my viswindow.cpp to visualize it in a Graph. In the mainwindow the right value is calculated and stored by pressing a button, which also opens the viswindow. I tried to pass it with a pointer or with signals and slots, but it wouldn't work.
mainwindow.cpp:
double MainWindow::berechneFussabdruck(double strecke, int anzahl, double sfcWert)
{
if(strecke <= 1500 && strecke > 0){
double aequivalente_Co2 = (((sfcWert*3.116*50*(((0.0011235955)*(strecke-2*48.42))+0.233))*0.001)/anzahl);
setAequivalente(aequivalente_Co2);
}
else{
double aequivalente_Co2 = (((sfcWert*3.116*75*(((0.0011235955)*(strecke-2*208))+0.833))*0.001)/anzahl);
setAequivalente(aequivalente_Co2);
}
return aequivalente_Co2;
}
Button to open viswindow:
void MainWindow::on_pushButton_clicked()
{
MainWindow::berechneFussabdruck(strecke, anzahl, sfcWert);
visWindow = new VisWindow(this);
visWindow->show();
}
viswindow.cpp:
VisWindow::VisWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::VisWindow)
{
ui->setupUi(this);
...
*set0 << aequivalente_Co2 << aequivalente_Co2;
*set1 << 11.07 << 0;
*set2 << 0 << 0.49;
...
}
Thanks for helping!

Here are examples of:
Single time value transfer from MainWindow to VisWindow in constructor parameter.
Value update from MainWindow to VisWindow by slot-signal chain.
Value update from MainWindow to VisWindow by direct call of VisWindow->updateValue() slot as regular function.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "viswindow.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
signals:
void updatevalue(const double value);
private:
Ui::MainWindow *ui;
VisWindow * m_viswindow;
};
#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_pushButton_clicked() {
m_viswindow = new VisWindow(12.34, nullptr);
connect(this, &MainWindow::updatevalue,
m_viswindow, &VisWindow::updateValue);
m_viswindow->show();
}
void MainWindow::on_pushButton_2_clicked() {
emit updatevalue(23.45);
}
void MainWindow::on_pushButton_3_clicked() {
m_viswindow->updateValue(45.67);
}
viswindow.h
#ifndef VISWINDOW_H
#define VISWINDOW_H
#include <QWidget>
namespace Ui {
class VisWindow;
}
class VisWindow : public QWidget
{
Q_OBJECT
public:
explicit VisWindow(const double value, QWidget *parent = nullptr);
~VisWindow();
public slots:
void updateValue(const double value);
private:
Ui::VisWindow *ui;
};
#endif // VISWINDOW_H
viswindow.cpp
#include "viswindow.h"
#include "ui_viswindow.h"
VisWindow::VisWindow(const double value, QWidget *parent) :
QWidget(parent),
ui(new Ui::VisWindow)
{
ui->setupUi(this);
ui->lblValue->setText(QString::number(value,'f',2));
this->setWindowTitle("VisWindow");
}
VisWindow::~VisWindow() {
delete ui;
}
void VisWindow::updateValue(const double value) {
ui->lblValue->setText(QString::number(value,'f',2));
}

Related

Get log from spdlog in qtextedit object from qt

I am trying to redirect my log in a qtextedit windows. I use this post : Redirecting std::cout from DLL in a separate thread to QTextEdit.
Here is my test program :
q_debugstream.h
#ifndef ThreadLogStream_H
#define ThreadLogStream_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QScrollBar>
#include "QTextEdit"
#include "QDateTime"
// MessageHandler
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
class MessageHandler : public QObject
{
Q_OBJECT
public :
MessageHandler(QTextEdit *textEdit, QObject * parent = Q_NULLPTR) : QObject(parent), m_textEdit(textEdit){}
public slots:
void catchMessage(QString msg)
{
this->m_textEdit->append(msg);
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
private:
QTextEdit * m_textEdit;
};
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
class ThreadLogStream : public QObject, public std::basic_streambuf<char>
{
Q_OBJECT
public:
ThreadLogStream(std::ostream &stream, QObject * parent = Q_NULLPTR) :QObject(parent), m_stream(stream)
{
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
~ThreadLogStream()
{
// output anything that is left
if (!m_string.empty())
{
emit sendLogString(QString::fromStdString(m_string));
}
m_stream.rdbuf(m_old_buf);
}
protected:
virtual int_type overflow(int_type v)
{
if (v == '\n')
{
emit sendLogString(QString::fromStdString(m_string));
m_string.erase(m_string.begin(), m_string.end());
}
else
m_string += v;
return v;
}
virtual std::streamsize xsputn(const char *p, std::streamsize n)
{
m_string.append(p, p + n);
long pos = 0;
while (pos != static_cast<long>(std::string::npos))
{
pos = static_cast<long>(m_string.find('\n'));
if (pos != static_cast<long>(std::string::npos))
{
std::string tmp(m_string.begin(), m_string.begin() + pos);
emit sendLogString(QString::fromStdString(tmp));
m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
}
}
return n;
}
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
std::string m_string;
signals:
void sendLogString(const QString& str);
};
#endif // ThreadLogStream_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QTextEdit>
#include "q_debugstream.h"
#include "spdlog/spdlog.h"
namespace Ui {
class MainWindow;
}
class ThreadWorker : public QObject
{
Q_OBJECT
public:
ThreadWorker()
{
}
private:
signals:
void finished();
public slots:
void doWork()
{
std::cout<< "Hello World2" <<std::endl;
qDebug() << "Hello World2q" ;
SPDLOG_INFO("Hello world2 spdlog");
emit finished();
}
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
// QMessage
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
static void QMessageOutput(QtMsgType , const QMessageLogContext &, const QString &msg);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
public slots:
void SingleThreadTest();
void MultiThreadTest();
private:
Ui::MainWindow *ui;
// MessageHandler for display and ThreadLogStream for redirecting cout
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
MessageHandler *msgHandler = Q_NULLPTR;
ThreadLogStream* m_qd;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <QThread>
#include "spdlog/spdlog.h"
// Catch QMessage, redirect to cout
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
void MainWindow::QMessageOutput(QtMsgType , const QMessageLogContext &, const QString &msg)
{
std::cout<<msg.toStdString().c_str()<<std::endl;
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Set up ThreadLogStream, which redirect cout to signal sendLogString
// Set up MessageHandler, wgucg catch message from sendLogString and Display
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
m_qd = new ThreadLogStream(std::cout); //Redirect Console output to QTextEdit
this->msgHandler = new MessageHandler(this->ui->textEdit, this);
connect(m_qd, &ThreadLogStream::sendLogString, msgHandler, &MessageHandler::catchMessage);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
connect(ui->pushButtonSingleThreadTest, SIGNAL(clicked()),this,SLOT(SingleThreadTest()));
connect(ui->pushButtonMultiThreadTest, SIGNAL(clicked()),this,SLOT(MultiThreadTest()));
}
void MainWindow::SingleThreadTest()
{
std::cout<< "Hello World1" <<std::endl;
qDebug() << "Hello World1q" ;
SPDLOG_INFO("Hello world1 spdlog");
}
void MainWindow::MultiThreadTest()
{
QThread *workerThread= new QThread;
ThreadWorker *worker = new ThreadWorker();
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(doWork()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "spdlog/spdlog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SPDLOG_INFO("Starting main");
MainWindow w;
w.show();
// Setup QMessageCatch
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
qInstallMessageHandler(MainWindow::QMessageOutput);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
return a.exec();
}
My problem : logs from SPDLOG_INFO appear in my console, but not in my qtextedit window. Obviously, the logs from spdlog are not catch by the function QMessageOutput from qInstallMessageHandler.
My question : Is it possible to catch the log from spdlog on a qtextedit box ?
In spdlog last versions have introduced qt_sinks feature. To use this include the header spdlog/sinks/qt_sinks then call a factory function (spdlog::qt_logger_mt("logger_name", ui->plainTextEdit)) that constructs st or mt(thread-safe) loggers. Also you are not limited to QTextEdit or QPlainTextEdit widgets so you can sink logs into QML, QUdpSocket and any custom widget or QObject based classes with your provided a slot name with a QString parameter.
Usage
custom_class.hpp
#ifndef CUSTOM_CLASS_HPP
#define CUSTOM_CLASS_HPP
#include <QObject>
class custom_class : public QObject
{
Q_OBJECT
public:
explicit custom_class(QObject *parent = nullptr);
public slots:
void log2qml(const QString &str); // slot for using spdlog with custom objects
signals:
void send2qml(const QString& str);
};
#endif // CUSTOM_CLASS_HPP
custom_class.cpp
#include "custom_class.hpp"
custom_class::custom_class(QObject *parent) : QObject(parent)
{
}
void custom_class::log2qml(const QString &str)
{
emit send2qml(str);
}
mainwindow.hpp:
#include "custom_class.hpp"
#include <QMainWindow>
#include <QThread>
#include <spdlog/sinks/qt_sinks.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void log();
private:
Ui::MainWindow *ui;
custom_class c;
QThread *th = nullptr;
std::shared_ptr<spdlog::logger> logger = nullptr, logger_custom = nullptr;
};
#endif // MAINWINDOW_HPP
mainwindow.cpp:
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "spdlog/spdlog.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// logger write into ui->plainTextEdit
logger = spdlog::qt_logger_mt("plaintextedit_logger", ui->plainTextEdit);
// custom_logger write into qml or any custom object. 3rd param is slot name that passes QString param in custom_object
logger_custom = spdlog::qt_logger_mt("custom_qml_logger", &c, "log2qml");
// set default logger
spdlog::set_default_logger(logger);
log();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::log()
{
th = QThread::create([this] {
for (int i = 0; i < 1000; ++i) {
SPDLOG_INFO(i); // default logger write into ui->plainTextEdit
logger_custom->warn(i); // custom_logger write into qml or any custom object
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
});
th->start();
}
output:

Dynamic QLabel misplacing in UI

I'm trying to create a program that accepts images through drag and drop and shows those images on my UI, then I want to rename them and save them.
I got the drag and drop to work, but I have some issues with my Image placement and I can't seem to find where I'm making my mistake.
In the image you see my UI during runtime, in the top left you can see a part of the image I dragged into the green zone(this is my drag and drop zone that accepts images). The position I actually want it to be in should be the red square. The green zone is a Dynamic created object called Imagehandler that I created to handle the drag and drop of the images. The Red square is my own class that inherits from QLabel, I called it myiconclass. This class should hold the actual image data.
I think my mistake has to do with the layouts, but I can't see it.
Could I get some help with this please?
Imagehandler.h
#ifndef IMAGEHANDLER_H
#define IMAGEHANDLER_H
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QList>
#include <QDebug>
//this class is designed to help me take in the images with drag and drop
class ImageHandler : public QWidget
{
Q_OBJECT
public:
explicit ImageHandler(QWidget *parent = nullptr);
QList<QImage> getImageListMemory() const;
void setImageListMemory(const QList<QImage> &value);
QList<QUrl> getUrlsMemory() const;
void setUrlsMemory(const QList<QUrl> &value);
private:
//QWidget Icon;
QLabel Icon;
QList <QImage> imageListMemory;
QList <QUrl> urlsMemory;
protected:
void dragEnterEvent(QDragEnterEvent * event);
void dragLeaveEvent(QDragLeaveEvent * event);
void dragMoveEvent(QDragMoveEvent * event);
void dropEvent(QDropEvent * event);
signals:
void transferImageSignal(QList <QImage>);
public slots:
};
#endif // IMAGEHANDLER_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImageReader>
#include <QList>
#include <QWidget>
#include <QLabel>
#include <myiconclass.h>
#include <imagehandler.h>
#include <QGridLayout>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QList<QImage> getImageListMemory() const;
void setImageListMemory(const QList<QImage> &value);
private:
Ui::MainWindow *ui;
QLabel Icon;
QList <QImage> imageListMemory;
QList <QUrl> urlsMemory;
QList<QWidget *> labelList;
ImageHandler * ImageHandlerMemory;
QGridLayout * grid2;
QList <MyIconClass *> memory;
signals:
public slots:
void setIconSlot(QList <QImage>);
};
#endif // MAINWINDOW_H
myiconclass.h
#ifndef MYICONCLASS_H
#define MYICONCLASS_H
#include <QWidget>
#include <QLabel>
//this class is based on a Qlabel and is only made so it can help me with the actual images, gives me more members if I need it
class MyIconClass : public QLabel
{
Q_OBJECT
public:
explicit MyIconClass(QWidget *parent = nullptr);
int getMyNumber() const;
void setMyNumber(int value);
private:
int myNumber;
signals:
public slots:
};
#endif // MYICONCLASS_H
imagehandler.cpp
#include "imagehandler.h"
ImageHandler::ImageHandler(QWidget *parent) : QWidget(parent)
{
setAcceptDrops(true);
}
QList<QImage> ImageHandler::getImageListMemory() const
{
return imageListMemory;
}
void ImageHandler::setImageListMemory(const QList<QImage> &value)
{
imageListMemory = value;
}
QList<QUrl> ImageHandler::getUrlsMemory() const
{
return urlsMemory;
}
void ImageHandler::setUrlsMemory(const QList<QUrl> &value)
{
urlsMemory = value;
}
void ImageHandler::dragEnterEvent(QDragEnterEvent * event)
{
event->accept();
}
void ImageHandler::dragLeaveEvent(QDragLeaveEvent * event)
{
event->accept();
}
void ImageHandler::dragMoveEvent(QDragMoveEvent * event)
{
event->accept();
}
void ImageHandler::dropEvent(QDropEvent * event)
{
QList <QImage> imageList2;
QList <QUrl> urls;
QList <QUrl>::iterator i;
urls = event->mimeData()->urls();
//imageList.append(event->mimeData()->imageData());
foreach (const QUrl &url, event->mimeData()->urls())
{
QString fileName = url.toLocalFile();
qDebug() << "Dropped file:" << fileName;
qDebug()<<url.toString();
QImage img;
if(img.load(fileName))
{
imageList2.append(img);
}
}
emit transferImageSignal(imageList2);
this->setUrlsMemory(urls);
this->setImageListMemory(imageList2);
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ImageHandler * handler = new ImageHandler(this);
handler->show();
QGridLayout *grid = new QGridLayout;
grid->addWidget(handler, 0, 0);
ui->groupBoxIcon->setLayout(grid);
ImageHandlerMemory = handler;
//connect(handler,SIGNAL(handler->transferImageSignal(QList <QUrl>)),this,SLOT(setIconSlot(QList <QUrl>)));
connect(handler,SIGNAL(transferImageSignal(QList<QImage>)),this,SLOT(setIconSlot(QList<QImage>)));
}
MainWindow::~MainWindow()
{
delete ui;
}
QList<QImage> MainWindow::getImageListMemory() const
{
return imageListMemory;
}
void MainWindow::setImageListMemory(const QList<QImage> &value)
{
imageListMemory = value;
}
void MainWindow::setIconSlot(QList<QImage> images)
{
printf("succes!");
this->setImageListMemory(images); //save the images to memory
QGridLayout *grid = new QGridLayout; //create the grid layout I want my images to be in
// create counters to remember the row and column in the grid
int counterRow =0;
int counterColumn =0;
int counter3 =0;
int counterImages = 0;
//iterate over each image in the list
QList <QImage>::iterator x;
for(x = imageListMemory.begin(); x != imageListMemory.end(); x++)
{
MyIconClass * myLabel = new MyIconClass(this); //create an object of my own class (which is a Qlabel with an int member)
QPixmap pixmap(QPixmap::fromImage(*x)); //create a pixmap from the image in the iteration
myLabel->setPixmap(pixmap); //set the pixmap on my label object
myLabel->show();
memory.append(myLabel); //add it to the memory so I can recal it
counterImages++;
}
while(counter3 < images.count())
{
grid2->addWidget(memory.value(counter3), counterRow, counterColumn);
counterColumn++;
counter3++;
if(counterColumn >= 5)
{
counterRow++;
counterColumn =0;
}
}
if(ImageHandlerMemory->layout() == 0)
{
ImageHandlerMemory->setLayout(grid2);
}
}
myiconclass.cpp
#include "myiconclass.h"
MyIconClass::MyIconClass(QWidget *parent) : QLabel(parent)
{
}
int MyIconClass::getMyNumber() const
{
return myNumber;
}
void MyIconClass::setMyNumber(int value)
{
myNumber = value;
}
As Benjamin T said I had to change this:
MyIconClass * myLabel = new MyIconClass(this);
into this:
MyIconClass * myLabel = new MyIconClass(ImageHandlerMemory);
Thanks Benjamin!
PS, I also had to add this line in my mainwindow constructor:
grid2 = new QGridLayout;

updating value from mainwindow to dialog

I have a mainwindow that generates random int numbers every second in "numbers" and I have a button that opens a new dialog. In that dialog is a button "Get Number" and a qlabel. If I press "Get Number" I want the current generated number from the mainwindow set to the dialog label text. I tried to write a member function in the mainwindow class that returns the actual value but I cant call it from the dialog and I think that's the wrong way. How can I get this current generated int "numbers" into the dialog? My plain code:
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 on_pushButton_clicked();
void generateNumbers();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(generateNumbers()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
Dialog dialog;
dialog.setModal(true);
dialog.exec();
}
void MainWindow::generateNumbers()
{
int numbers = qrand() % 100;
qDebug() << numbers;
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
On mainwindow.h, add a signal:
signals:
void update_number(int);
On mainwindow.cpp, emit that signal everytime you generate a new number:
void MainWindow::generateNumbers()
{
int numbers = qrand() % 100;
emit update_number(numbers); // signals that a new number has been generated
qDebug() << numbers;
}
and your on_pushButton_clicked() should look like this:
void MainWindow::on_pushButton_clicked()
{
Dialog dialog(this); // main window as the parent of dialog
dialog.setModal(true);
dialog.exec();
}
On dialog.h, add an attribute called number_ and add the following slot:
public slots:
void on_update_number(int number); // updates the number_
private:
int number_; // it will be updated with the most recent number
Ui::Dialog *ui;
On dialog.cpp, connect the signal to the slot:
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(parent, SIGNAL(update_number(int)), this, SLOT(on_update_number(int)));
}
add the slot code:
void Dialog::on_update_number(int number)
{
number_ = number;
}
Finally, when the user clicks the button "Get Number" in the dialog, set the label text:
void Dialog::on_pushButton_clicked()
{
ui->label->setText(QString::number(number_));
}
For more information on Signals & Slots go here.

missing derived class with QObject

I am getting the error ~mainwindow.cpp:9: error: redefinition of 'MainWindow::MainWindow(QWidget*)' MainWindow::MainWindow(QWidget *parent) : and I am pretty sure it is complaining about the wrong derived class name. The base class is MainWindow but I do not see what the derived class would be.
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QImageReader>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void coreEng::requestPage(){
QUrl url("http://www.nyctelecomm.com");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
}
void coreEng::finishedSlot(QNetworkReply* reply){
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
QImage pic = imageReader.read();
QByteArray bytes = reply->readAll(); // bytes
QString string(bytes); // string
}
else
{
}
// delete reply();
}
void MainWindow::on_lcdNumber_overflow()
{
QByteArray replyData = netReply->readAll();
}
void MainWindow::on_pushButton_clicked()
{
QObject::on_pushButton_clicked(nam, SIGNAL(finished(QNetworkReply*)),
this, SLOT(finishedSlot(QNetworkReply*)));
}
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QObject>
#include <QNetworkAccessManager>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0):
QMainWindow(parent)
{
nam = new QNetworkAccessManager();
}
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
//void connect();
void on_pushButton_clicked();
void requestPage();
void finishedSlot(QNetworkReply* reply);
private slots:
void on_lcdNumber_overflow();
private:
QNetworkAccessManager* nam;
};
#endif // MAINWINDOW_H
//main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow mConnect;
mConnect.on_pushButton_clicked();
mConnect.requestPage();
MainWindow w;
w.show();
return a.exec();
}
You have defined MainWindow::MainWindow two times. First in header file and second in source file. That is said in error message.
explicit MainWindow(QWidget *parent = 0):
QMainWindow(parent)
{
nam = new QNetworkAccessManager();
}
//
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

QT : Memory Leakage and SubClassing (Custom QWidget) Issue

I have an mainwindow app, with 3 main modules and each modules have 6 sub-modules. Each submodel consists of different kind of Gui widgets (like Qlineedit, Qlabel, QPushbutton,QGroupBox etc…).
I would subclass all each main and sub-modules (inheriths QWidget). Each main and sub module is created dynamically. For ex, when the 1st main module is constructed, it also allocates for the 6 submodules. But, when the app is closed, it doesn’t free all 6 submodules. There may be a leakage problem but I didn’t figure out. why. I think I have a problem with subclassing of my modules.
My app is too big to post here so I would post 2 benchmark codes. To show the issue, this mainwindow app has 100 my custom QLineEdit object (I know it is weird but it adresses my problem well). But when the app is closed, doesn’t free all the 100 custom Qlineedit objects. (Here, my custom QLineedit class represents my modules…)
I have tried 2 kind of subclassing to understand the problem. 1. BcInputBox inherits QLineEdit 2. BcInputBox inherits QWidget
I’ve tried both of them separately but the issue is the same. I’ve spent more than 1 week but I haven’t figured it out yet.
Which approach would be better? What is the mistake about my design?
1st Benchmark
// #####################################################
// Benchmark 1 //
// #####################################################
//
//BcInputBox.h
#ifndef BCINPUTBOX_H
#define BBCINPUTBOX_H
#include <QLineEdit>
namespace BC {
const bool ReadOnly = true;
const bool Edit = false;
const bool Double = true;
const bool Name = false;
}
class BcInputBox: public QWidget
{
Q_OBJECT
public:
explicit BcInputBox(QWidget *parent = 0, bool editInfo = BC::Edit, bool inputInfo = BC::Double);
~BcInputBox();
void setEditInfo(bool editInfo);
void setInputInfo(bool inputInfo);
QLineEdit *getInputBox() const;
static int objCounter;
private:
QLineEdit *inputBox;
};
#endif // BCINPUTBOX_H
//******************************************
//BcInputBox.cpp
#include <QDebug>
#include <QHBoxLayout>
#include <QValidator>
#include "BcInputBox.h"
#include "BcDoubleValidator.h"
int BcInputBox :: objCounter = 0;
BcInputBox::BcInputBox(QWidget *parent, bool editInfo, bool inputInfo):QWidget(parent)
{
QHBoxLayout *hlay = new QHBoxLayout(this);
inputBox = new QLineEdit(this);
setEditInfo(editInfo);
setInputInfo(inputInfo);
hlay -> addWidget(inputBox);
inputBox -> setStyleSheet("background-color: yellow;"
"border-radius: 8px;");
qDebug() << objCounter++ << "BcInputBox()";
}
BcInputBox :: ~BcInputBox()
{
//qDebug() << objCounter ;
qDebug() << "~BcInputBox";
delete inputBox;
}
void BcInputBox :: setEditInfo(bool editInfo)
{
inputBox -> setReadOnly(editInfo);
}
void BcInputBox :: setInputInfo(bool inputInfo)
{
if (!inputInfo){
QRegExp rExp ("[A-Za-z0-9]{1,16}");
inputBox -> setValidator(new QRegExpValidator(rExp, inputBox));
}
else {
inputBox -> setValidator( new BcDoubleValidator( 0.0, 1000, 3,inputBox));
}
}
QLineEdit *BcInputBox :: getInputBox() const
{
return inputBox;
}
//******************************************
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include "subconsmod1.h"
#include "BcInputBox.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
BcInputBox *subSection[100];
};
#endif // MAINWINDOW_H
//******************************************
//MainWindow.cpp
#include <QtCore>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *hlay = new QVBoxLayout(ui->centralWidget);
for (int i = 0; i < 100; ++i ){
subSection[i] = new BcInputBox (ui->centralWidget);
hlay -> addWidget(subSection[i]);}
ui->centralWidget ->setLayout(hlay);
}
MainWindow::~MainWindow()
{
delete ui;
}
2nd Benchmark
// #####################################################
// Benchmark 2 //
// #####################################################
//
//BcInput.h
#ifndef BCINPUT_H
#define BCINPUT_H
#include <QLineEdit>
class BcInput : public QLineEdit
{
Q_OBJECT
public:
explicit BcInput(QWidget *parent = 0);
~BcInput();
static int counter;
signals:
public slots:
};
#endif // BCINPUT_H
//******************************************
//BcInput.cpp
#include <QDebug>
#include "bcinput.h"
int BcInput :: counter = 0;
BcInput::BcInput(QWidget *parent) :
QLineEdit(parent)
{
qDebug() << "BcInput()";
qDebug() << counter++;
}
BcInput :: ~BcInput()
{
qDebug() << "~BcInput()";
}
//******************************************
//MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "bcinput.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
BcInput *subSection[100];
};
#endif // MAINWINDOW_H
//******************************************
//MainWindow.cpp
#include <QtGui>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bcinput.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QVBoxLayout *hlay = new QVBoxLayout(ui->centralWidget);
for(int i = 0; i<100;i++){
subSection[i] = new BcInput(ui->centralWidget);
hlay->addWidget(subSection[i]);}
ui->centralWidget->setLayout(hlay);
}
MainWindow::~MainWindow()
{
for(int i = 0; i<100;i++)
delete subSection[i];
delete ui;
}