How can I fix this error? - c++

I came across this error when I tried to run this example code while reading the book - "C++ GUI programming with Qt".
gotocelldialog.h :
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"
class gotocelldialog : public QDialog, public Ui::goToCellDialog
{
Q_OBJECT
public:
explicit gotocelldialog(QWidget *parent = 0);
signals:
public slots:
void on_lineEdit_textChanged();
};
#endif // GOTOCELLDIALOG_H
gotocelldialog.cpp :
#include "gotocelldialog.h"
#include <QtGui>
gotocelldialog::gotocelldialog(QWidget *parent) :
QDialog(parent)
{
setupUi(this);
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp,this));
connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
connect(cancelButton,SIGNAL(clicked()),this,SLOT(accept()));
}
void gotocelldialog::on_lineEdit_textChanged(){
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
main.cpp:
#include <QApplication>
#include <QDialog>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
gotocelldialog* dialog = new gotocelldialog();
dialog->show();
return a.exec();
}
When I run this program , it displays this error message :
/home/ayush/untitled3/gotocelldialog.cpp:6: error: no matching function for call to 'gotocelldialog::setupUi(gotocelldialog*)'
setupUi(this);
^
../untitled3/gotocelldialog.cpp:6:17: note: candidate is:
In file included from ../untitled3/gotocelldialog.h:5:0,
from ../untitled3/gotocelldialog.cpp:1:
./ui_gotocelldialog.h:49:10: note: void Ui_goToCellDialog::setupUi(QMainWindow*)
void setupUi(QMainWindow *goToCellDialog)
^
./ui_gotocelldialog.h:49:10: note: no known conversion for argument 1 from 'gotocelldialog*' to 'QMainWindow*'
Makefile:344: recipe for target 'gotocelldialog.o' failed
make: *** [gotocelldialog.o] Error 1
17:15:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled3 (kit: Desktop)
When executing step 'Make'
What is the reason for this error and how can this be solved ?

Looks like your ui file defines a QMainWindow, but that your code expects a QDialog.
So there's a mismatch, your should probably recreate the ui file from the QDialog template, or change it in an editor.

Related

What is no matching function call error in Qt

I created a class, whose base class was QObject, Now if I try to add a QTextBrowser in my code, I get the error as no matching function for call to QTextBrowser. I tried to compile the code by adding QWidget class but still I am unable to resolve the error. How can I resolve this.
MainWindow code
#include <QApplication>
#include "window.h"
#include "bbbserver.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
bbbServer server;
Window window;
window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();
return app.exec();
}
Here is the code for window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
class QPushButton;
class QTextBrowser;
class QProcess;
class QFile;
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
QTextBrowser *statusWindow;
private slots:
};
#endif // WINDOW_H
Here is the code for window.cpp
#include "window.h"
#include <QPushButton>
#include <QProcess>
#include <QTextBrowser>
#include <QDebug>
Window::Window(QWidget *parent) : QWidget(parent)
{
// Create and position the buttons on the main window
/*************** text browser *********************/
statusWindow = new QTextBrowser(this);
statusWindow->setMinimumSize(QSize(0,0));
statusWindow->setMaximumSize(QSize(10000,10000));
statusWindow->setGeometry(175, 50, 440, 420);
statusWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
}
Here is the code for bbbserver.h file
#ifndef BBBSERVER_H
#define BBBSERVER_H
#include <QObject>
#include <QWidget>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class bbbServer : public QObject
{
Q_OBJECT
public:
explicit bbbServer(QObject *parent = 0);
signals:
public slots:
void newConnection();
private:
QTcpServer *server;
};
#endif // BBBSERVER_H
this is bbbserver.cpp file
#include "bbbserver.h"
bbbServer::bbbServer(QObject *parent):
QObject(parent)
{
/*************************** SERVER *********************************/
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!server->listen(QHostAddress::QHostAddress("192.168.0.1"), 5000))
{
qDebug() << "SERVER NOT STARTED";
}
else
{
qDebug() << "SERVER STARTED";
}
}
void bbbServer::newConnection()
{
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Connection from 192.168.0.1 BBB\n");
socket->flush();
socket->waitForBytesWritten(30000);
socket->waitForReadyRead(30000);
qDebug() << socket->readAll();
`HERE I WANT TO ACCESS THE STATUS WINDOW(textbrowser statusWindow)`
}
And this is the full error, which is same as in screenshot.
bbbserver.cpp:8: error: no matching function for call to 'QTextBrowser::QTextBrowser(bbbServer* const)'
The parent parameter passed to the QTextBrowser constructor needs to be of type QWidget * whereas you're passing a QObject *. If you really need the bbbServer to be the parent object of the QTextBrowser then simply do...
infoSpace = new QTextBrowser;
infoSpace->QObject::setParent(this);
Note: The QObject:: qualifier is required because QWidget::setParent(QWidget *) effectively hides its base's QObject::setParent(QObject *) member meaning the latter won't take part in the normal lookup process.

C++/Qt Q_OBJECT macro causes an error

I've just started programming in Qt framework. Following is a very simple program:
#include <QtCore/QCoreApplication>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass() {}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClass *c = new MyClass();
return a.exec();
}
But I receive following error when I try to compile & run it:
In function MyClass:
undefined reference to vtable for MyClass
But when I remove the QObject macro everything works fine. Please note that the class is defined in the same file as the main function.
I'm using Qt version 4.7, running on Win 7.
What is causing this issue?
Update: I get the same error when I define my class in a separate header file. mytimer.h:
#ifndef MYTIMER_H
#define MYTIMER_H
#include <QtCore>
class MyTimer : public QObject
{
Q_OBJECT
public:
QTimer *timer;
MyTimer();
public slots:
void DisplayMessage();
};
#endif // MYTIMER_H
mytimer.cpp:
#include "mytimer.h"
#include <QtCore>
MyTimer::MyTimer()
{
timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(DisplayMessage()));
timer->start(1000);
}
void MyTimer::DisplayMessage()
{
qDebug() << "timed out";
}
And this is the main.cpp:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include "mytimer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyTimer *mt = new MyTimer();
return a.exec();
}
You need to compile it using qmake, which is going to create mock methods for your custom QObject class. See here about more on generating moc files.
Since your example doesn't contain header files, it is not parsed, and no moc files are generated. You need to declare MyClass in a separate header file, and run moc generation tool.
When you are using QT Creator you should cleanup up your project and execute qmake, in the build menu.
Whenever u apply some changes first clean your project, then run qmake, and the finally build your project...

"collect2: ld returned 1 exit status" in Qt creator

When I compile my program, I get ld returned 1 exit status error. Here is the code (main.cpp):
#include <QtGui/QApplication>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLineEdit>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton *okayButtom = new QPushButton("Okay");
QMainWin *mainWin = new QMainWin("Say Pig!");
QHBoxLayout *Hbox = new QHBoxLayout;
QLineEdit *inputBox = new QLineEdit;
mainWin->setWindowTitle("Hello, Pig!");
Hbox->addWidget(inputBox);
Hbox->addWidget(okayButtom);
mainWin->setLayout(Hbox);
mainWin->show();
return app.exec();
}
and this is widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
class QMainWin : public QWidget
{
Q_OBJECT
private:
QString WinTitle;
public:
QMainWin(const QString &title, QWidget *parent = 0):WinTitle(title)
{
this->setWindowTitle(WinTitle);
}
~QMainWin();
};
#endif // WIDGET_H
I didn't use UI file, I only use cpp source file.
define a body for the destructor or remove the declaration ~QMainWin();
remove the macros Q_OBJECT;
add QWidget(parent) to the initializing list at the
constructor.
read this http://doc.trolltech.com/4.3/tutorial-t4.html

C++ Qt - strange behavior when Class T's attribute has parent is T

Not sure this issue about Qt or C++ in general, I'm just a newbie for both of these!
I got a simple Qt app, with a MainWindow and Hello class like below:
hello.h
#ifndef HELLO_H
#define HELLO_H
#include <QWidget>
#include "mainwindow.h"
class Hello : public QWidget
{
Q_OBJECT
public:
explicit Hello(MainWindow *parent = 0);
signals:
public slots:
};
#endif // HELLO_H
heloo.cpp
#include "hello.h"
Hello::Hello(MainWindow *parent) :
QWidget(parent)
{
//nothing here yet
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "hello.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Hello* hi;
};
#endif // MAINWINDOW_H
mainwindows.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
hi = new Hello(this);
}
MainWindow::~MainWindow()
{
}
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
and here is the error when I build my project:
from ../untitled1/main.cpp:2: ../untitled1/hello.h:11: error: expected
‘)’ before ‘*’ token
and the line cause the error is:
explicit Hello(MainWindow *parent = 0);
Can you help me to resolve the issue!
Thanks you!
You have circular inclusion of header files in "hello.h" and "maindwindow.h". There is no need to include these files in the header file as you are just using a pointer. A simple forward declaration such as class MainWindow; in "hello.h" is sufficient.

Qt - moc file related error

I am coding a project. I started to getting errors after I tried to make the static build. I made some changes, which I can't remember. But, I am sure that if this stub can be cleared, then the main project also can be get cleared.
This is header file.
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QFileDialog>
#include <QLabel>
#include <QMouseEvent>
#include <QObject>
#include <QPaintEvent>
class MyLabel:public QWidget
{
private:
QPixmap default_Pixmap;
QPixmap pixmap;
QFileDialog * fileDialog;
Q_OBJECT
public:
MyLabel();
void setPixmap(QPixmap pixmap);
void setDefault();
protected:
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent * event);
signals:
void file_Selected(QString fileName);
private slots:
void file_Got_Selected(QString fileName);
};
#endif // MYLABEL_H
Here is the source file
#include "MyLabel.h"
#include "MyMessageBox.h"
#include <QFileDialog>
#include <QPainter>
MyLabel::MyLabel():QWidget()
{
default_Pixmap = QPixmap("select.gif").scaled(250,100);
this->fileDialog=new QFileDialog(this);
fileDialog->setNameFilter("Image Files (*.BMP *.GIF *.JPG *.JPEG *.PNG *.PBM *.PGM *.PPM *.XBM *.XPM)");
connect(fileDialog,SIGNAL(fileSelected(QString)),this,SIGNAL(file_Selected(QString)));
connect(fileDialog,SIGNAL(fileSelected(QString)),this,SLOT(file_Got_Selected(QString)));
}
void MyLabel::setPixmap(QPixmap pixmap)
{
this->pixmap = pixmap;
}
void MyLabel::setDefault()
{
this->pixmap = default_Pixmap;
}
void MyLabel::mousePressEvent(QMouseEvent *event)
{
fileDialog->show();
//QString file_Name = file_Dialog.getOpenFileName();
}
void MyLabel::paintEvent(QPaintEvent * event)
{
QPainter painter(this);
painter.drawPixmap(0,0,width(),height(),pixmap.scaled(QSize(width(),height())));
}
void MyLabel::file_Got_Selected(QString fileName)
{
this->pixmap = QPixmap(fileName);
}
#include "myLabel.moc"
And this is the main file
#include <QLabel>
#include <QPixmap>
#include <QtGui/QApplication>
#include "myLabel.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyLabel mm;
//mm.setPixmap(QPixmap("dummy.jpg"));
//mm.setPixmap(QPixmap());
mm.setDefault();
mm.show();
return a.exec();
}
I created the moc file using qt command prompt and the command
moc myLabel.h -o myLabel.moc
After this I tried to compile the project through Qt-Editor. But I got multi definition error as follows,
debug/moc_myLabel.o:d:/TempInstallationFolder/Qt/Dynamic/qt/include/QtCore/../../src/corelib/global/qglobal.h:1381: multiple definition of `MyLabel::metaObject() const'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:57: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:62: multiple definition of `MyLabel::qt_metacast(char const*)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:62: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:70: multiple definition of `MyLabel::qt_metacall(QMetaObject::Call, int, void**)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:70: first defined here
debug/moc_myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/debug/moc_myLabel.cpp:87: multiple definition of `MyLabel::file_Selected(QString)'
debug/myLabel.o:C:\Documents and Settings\prabhakaran\Desktop\CalendarNew-build-desktop/../CalendarNew//myLabel.moc:87: first defined here
debug/moc_myLabel.o:moc_myLabel.cpp:(.data+0x0): multiple definition of `MyLabel::staticMetaObject'
debug/myLabel.o:myLabel.cpp:(.data+0x0): first defined here
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\CalendarNew.exe] Error 1
mingw32-make: * [debug] Error 2
The process "D:/TempInstallationFolder/Qt/Dynamic/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project CalendarNew (target: Desktop)
When executing build step 'Make'
Anybody please hep me out of this problem.
Try to remove line include "MyLabel.moc" form you source file. You don't need to include it into cpp file.