QT: unresolved external symbol while trying to get webpage source code - qt

I started developing with Qt and I'm trying to get webpage source code. Here's what I have :
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
/**/
class GetHTMLSource : public QObject
{
Q_OBJECT
public:
GetHTMLSource();
void GetSource();
public slots:
void GetDone(QNetworkReply*);
private:
QNetworkAccessManager* NetManager;
};
GetHTMLSource::GetHTMLSource()
{
NetManager = new QNetworkAccessManager(this);
connect(NetManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(GetDone(QNetworkReply*)));
}
void GetHTMLSource::GetSource()
{
NetManager->get(QNetworkRequest(QUrl("http://stackoverflow.com")));
}
void GetHTMLSource::GetDone(QNetworkReply* ReplyIn)
{
QByteArray DataIn=ReplyIn->readAll();
QString DataString(DataIn);
//process str any way you like!
}
/**/
void MainWindow::on_pushButton_clicked()
{
GetHTMLSource Test;
Test.GetSource();
}
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();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
.pro
QT += core gui
TARGET = TestGetPageSource
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
QT += network
Errors are :
mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public:
virtual struct QMetaObject const * __thiscall
GetHTMLSource::metaObject(void)const "
(?metaObject#GetHTMLSource##UBEPBUQMetaObject##XZ)
mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public:
virtual void * __thiscall GetHTMLSource::qt_metacast(char const *)"
(?qt_metacast#GetHTMLSource##UAEPAXPBD#Z)
mainwindow.obj:-1: error:LNK2001: unresolved external symbol "public:
virtual int __thiscall GetHTMLSource::qt_metacall(enum
QMetaObject::Call,int,void * *)"
(?qt_metacall#GetHTMLSource##UAEHW4Call#QMetaObject##HPAPAX#Z)

Generally if you create classes derived from QObject you have to put them in separate header and source files.
So one .h and one .cpp file for one class.

Fixed it on my own. Of course it was my stupid mistake.
In class definition :
public slots:
void GetDone(QNetworkReply*);
was not matching actual function :
void GetHTMLSource::GetDone(QNetworkReply* ReplyIn)
{ ... }
However moving it to seperated .h and .cpp files probably helped.

Related

Qt with OpenCV: LNK2019:unresolved external symbol and File not Found mainwindow.obj

I am trying to build an example qt-opencv project Hello_Qt_OpenCVthat is provided by Computer-Vision-with-OpenCV-3-and-Qt5. I use QtCreator4.5 with Qt5.7 and opencv3.2. Their configurations work fine with some other qt-opencv projects.
Building the project gives three issues:
File not found: mainwindow.obj: LNK2019: unresolved external symbol "void __cdecl cv::medianBlur(class cv::_InputArray const &,class cv::_OutputArray const &,int)" (?medianBlur#cv##YAXAEBV_InputArray#1#AEBV_OutputArray#1#H#Z) referenced in function "private: void __cdecl MainWindow::on_outputPushButton_pressed(void)" (?on_outputPushButton_pressed#MainWindow##AEAAXXZ)
File not found: mainwindow.obj: LNK2019: unresolved external symbol "void __cdecl cv::GaussianBlur(class cv::_InputArray const &,class cv::OutputArray const &,class cv::Size,double,double,int)" (?GaussianBlur#cv##YAXAEBV_InputArray#1#AEBV_OutputArray#1#V?$Size_#H#1#NNH#Z) referenced in function "private: void __cdecl MainWindow::on_outputPushButton_pressed(void)" (?on_outputPushButton_pressed#MainWindow##AEAAXXZ)
LNK1120: 2 unresolved externals release\Hello_Qt_OpenCV.exe:-1: error
I tried some suggestions at (File not found: mainwindow.obj by clean, run qmake and build, which however won't work in this case.
For your information, I post the mainwindow.cpp and mainwindow.h:
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
loadSettings();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_inputPushButton_pressed()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open Input Image", QDir::currentPath(), "Images (*.jpg *.png *.bmp)");
if(QFile::exists(fileName))
{
ui->inputLineEdit->setText(fileName);
}
}
void MainWindow::on_outputPushButton_pressed()
{
QString fileName = QFileDialog::getSaveFileName(this, "Select Output Image", QDir::currentPath(), "*.jpg;;*.png;;*.bmp");
if(!fileName.isEmpty())
{
ui->outputLineEdit->setText(fileName);
using namespace cv;
Mat inpImg, outImg;
inpImg = imread(ui->inputLineEdit->text().toStdString());
if(ui->medianBlurRadioButton->isChecked())
cv::medianBlur(inpImg, outImg, 5);
else if(ui->gaussianBlurRadioButton->isChecked())
cv::GaussianBlur(inpImg, outImg, Size(5, 5), 1.25);
imwrite(fileName.toStdString(), outImg);
if(ui->displayImageCheckBox->isChecked())
imshow("Output Image", outImg);
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
int result = QMessageBox::warning(this, "Exit", "Are you sure you want to close this program?", QMessageBox::Yes, QMessageBox::No);
if(result == QMessageBox::Yes)
{
saveSettings();
event->accept();
}
else
{
event->ignore();
}
}
void MainWindow::loadSettings()
{
QSettings settings("Packt", "Hello_OpenCV_Qt", this);
ui->inputLineEdit->setText(settings.value("inputLineEdit", "").toString());
ui->outputLineEdit->setText(settings.value("outputLineEdit", "").toString());
ui->medianBlurRadioButton->setChecked(settings.value("medianBlurRadioButton", true).toBool());
ui->gaussianBlurRadioButton->setChecked(settings.value("gaussianBlurRadioButton", false).toBool());
ui->displayImageCheckBox->setChecked(settings.value("displayImageCheckBox", false).toBool());
}
void MainWindow::saveSettings()
{
QSettings settings("Packt", "Hello_OpenCV_Qt", this);
settings.setValue("inputLineEdit", ui->inputLineEdit->text());
settings.setValue("outputLineEdit", ui->outputLineEdit->text());
settings.setValue("medianBlurRadioButton", ui->medianBlurRadioButton->isChecked());
settings.setValue("gaussianBlurRadioButton", ui->gaussianBlurRadioButton->isChecked());
settings.setValue("displayImageCheckBox", ui->displayImageCheckBox->isChecked());
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QDir>
#include <QFile>
#include <QCloseEvent>
#include <QMessageBox>
#include <QSettings>
#include "opencv2/opencv.hpp"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void closeEvent(QCloseEvent *event);
private slots:
void on_inputPushButton_pressed();
void on_outputPushButton_pressed();
private:
Ui::MainWindow *ui;
void loadSettings();
void saveSettings();
};
#endif // MAINWINDOW_H
You may also download the project files and give a quick run to see if the issues are related to the project itself.
Add imgproc320 in the .pro file, and then the problem is solved

Connecting Signals and Slots between Child and Parent (invalid use of incomplete type)

I'm trying to connect signals from a child window (QDialog named VolumePage) to its parent (QMainWindow named MockUI). I'm running into a:
invalid use of incomplete type 'class Ui::VolumePage'
error when I attempt to make the connection.
I'm trying to connect inside the MockUI.cpp when I make the volume page. (Happens on a button press)
void MockUI::on_pushButton_3_clicked()
{
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
I've made Ui::VolumePage *ui; public.
Here's the entire error:
error: invalid use of incomplete type 'class Ui::VolumePage'
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
Can anyone help me understand what the problem is, or another clean way to do what I'm trying to do?
Edit: (Additional Source)
VolumePage.h:
#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H
#include <QDialog>
namespace Ui {
class VolumePage;
}
class VolumePage : public QDialog
{
Q_OBJECT
public:
explicit VolumePage(QWidget *parent = 0);
Ui::VolumePage *ui;
~VolumePage();
private slots:
void on_verticalSlider_valueChanged(int value);
private:
};
#endif // VOLUMEPAGE_H
mockUI.cpp:
#include "mockUI.h"
#include "ui_mock_ics.h"
#include <QLCDNumber>
#include "volumepage.h"
MockUI::MockUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MockUI)
{
ui->setupUi(this);
}
MockUI::~MockUI()
{
delete ui;
}
void MockUI::on_pushButton_3_clicked()
{
//Modal Approach
//VolumePage volPage;
//volPage.setModal(true);
//volPage.exec();
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage->ui->verticalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
volumepage.cpp:
#include "volumepage.h"
#include "mockUI.h"
#include "ui_volumepage.h"
VolumePage::VolumePage(QWidget *parent) :
QDialog(parent),
ui(new Ui::VolumePage)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
//Mock_ICS* ics = (Mock_ICS*)parent;
//connect(ui->verticalSlider,SIGNAL(valueChanged(int)),ics->ui->progressBar,SLOT(setValue(int)));
//connect(ui->verticalSlider,SIGNAL(valueChanged(int)),this->parent()->progressBar,SLOT(setValue(int)));
}
VolumePage::~VolumePage()
{
delete ui;
}
Ui::VolumePage *ui should remain private. Instead create a signal on the volPage, something like verticalSliderValueChanged(int value). See below...
mockui.h
#ifndef MOCKUI_H
#define MOCKUI_H
#include <QMainWindow>
#include "volumepage.h"
namespace Ui {
class MockUI;
}
class MockUI : public QMainWindow
{
Q_OBJECT
public:
explicit MockUI(QWidget *parent = 0);
~MockUI();
VolumePage* volPage;
private slots:
void on_pushButton_3_clicked();
private:
Ui::MockUI *ui;
};
#endif // MOCKUI_H
mockui.cpp
#include "mockui.h"
#include "ui_mockui.h"
#include "volumepage.h"
MockUI::MockUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MockUI)
{
ui->setupUi(this);
}
MockUI::~MockUI()
{
delete ui;
}
void MockUI::on_pushButton_3_clicked()
{
//Non-Modal Approach
volPage = new VolumePage(this);
volPage->show();
connect(volPage,SIGNAL(verticalSliderValueChanged(int)),ui->progressBar,SLOT(setValue(int)));
}
volumepage.h
#ifndef VOLUMEPAGE_H
#define VOLUMEPAGE_H
#include <QDialog>
namespace Ui {
class VolumePage;
}
class VolumePage : public QDialog
{
Q_OBJECT
public:
explicit VolumePage(QWidget *parent = 0);
~VolumePage();
signals:
void verticalSliderValueChanged(int value);
private slots:
void on_verticalSlider_valueChanged(int value);
private:
Ui::VolumePage *ui;
};
#endif // VOLUMEPAGE_H
volumepage.cpp
#include "volumepage.h"
#include "ui_volumepage.h"
VolumePage::VolumePage(QWidget *parent) :
QDialog(parent),
ui(new Ui::VolumePage)
{
ui->setupUi(this);
}
VolumePage::~VolumePage()
{
delete ui;
}
void VolumePage::on_verticalSlider_valueChanged(int value)
{
emit verticalSliderValueChanged(value);
}
This is an example of the pImpl idiom in C++. Class VolumePage is the one you should be using - this is the "public" interface of what Qt Creator has generated. Ui::VolumePage contains the complicated details you should not worry about.
So, in your code you should be using VolumePage, not Ui::VolumePage. You shouldn't have make Ui::VolumePage public.

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 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: QObject::connect: Cannot connect (null)

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()));

Resources