I'm a beginner in Qt when I run the following code I got these errors:
no void MainWindow::mousePressEvent(QMouseEvent *f) member function declared in class 'mainwindow'.
no void void MainWindow::paintEvent(QPaintEvent *e) member function declared in class 'mainwindow'.
The code is written in main.cpp file and I didn't write anything in mainwindow.cpp or mainwindow.h
The Qt code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
void MainWindow::mousePressEvent(QMouseEvent *f)
{
QPoint point=f->pos();
int y=1;
update();
}
void MainWindow::paintEvent(QPaintEvent *e)
{
int y;
QPoint point;
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setCapStyle(Qt::RoundCap);
linepen.setWidth(30);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(linepen);
if(y==1)
painter.drawPoint(point);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
In header should be:
protected:
void mousePressEvent(QMouseEvent *f);
void paintEvent(QPaintEvent *e);
And includes:
#include <QMouseEvent>
#include <QPaintEvent>
Also you should write your code in mainwindow.cpp(paintEvent, and another member functions). If you will have many classes, then your main.cpp can be very hard-readable.
Related
This happens when i enter the code to the slots of my buttons
when i debug the app normally ,all functions etc everything is ok.
but when i try use my code in UI something brokes down.
I'm making the Parking System for my uni classes ,
i got one parent class vehicle and 4 child classes which inherits by public.
here's the code from the UI source file :
#include "datain.h"
#include "ui_datain.h"
#include <QMessageBox>
#include <car.h>
#include <bike.h>
#include <motorbike.h>
#include <tir.h>
#include <vehicle.h>
#include <iostream>
using namespace std;
dataIn::dataIn(QWidget *parent) :
QDialog(parent),
ui(new Ui::dataIn)
{
ui->setupUi(this);
}
dataIn::~dataIn()
{
delete ui;
}
dataIn siema;
void dataIn::on_lineEdit_textEdited(const QString &arg1)
{
siema.rejestracja=arg1;
}
void dataIn::on_lineEdit_2_textEdited(const QString &arg1)
{
siema.marka=arg1;
}
void dataIn::on_car_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Car obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_motorcycle_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Motorbike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_bike_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Bike obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
void dataIn::on_tir_clicked()
{
Vehicle *wsk;
string reje,mareczka;
reje=siema.rejestracja.toStdString();
mareczka=siema.marka.toStdString();
Tir obj(reje,mareczka);
wsk=&obj;
wsk->add();
}
Here's the main:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
dataIn code:
#ifndef DATAIN_H
#define DATAIN_H
#include <QString>
#include <iostream>
#include <QDialog>
using namespace std;
namespace Ui {
class dataIn;
}
class dataIn : public QDialog
{
Q_OBJECT
QString rejestracja;
QString marka;
public:
explicit dataIn(QWidget *parent = 0);
~dataIn();
private slots:
void on_lineEdit_textEdited(const QString &arg1);
void on_lineEdit_2_textEdited(const QString &arg1);
void on_car_clicked();
void on_motorcycle_clicked();
void on_bike_clicked();
void on_tir_clicked();
private:
Ui::dataIn *ui;
};
#endif // DATAIN_H
You may not declare
dataIn siema;
and you should not refer to it - remove all siema.... references from your dataIn functions (replace siema. with nothing or with this->).
When you need your dialog for the first time, create it in (and as child of) MainWindow:
void MainWindow::whateverEvent() {
dataIn *siema = new dataIn(this);
siema->show();
}
If you need to reference it again or want do prevent it from being created twice, store the pointer in a member of your MainWindow.
i am working on QT C++,developing an application that display an image on a label(control) and after click that image the new image will display in other label (control). i create a new project: application->QtWidget Application. then i select class Qdialog. in project file are
- my_qlabel_mouseEvent.pro
and in header folder are
- dialog.h
- my_qlabel.h
and then source folder has files
1: dialog.cpp
2:main.cpp
3: my_qlabel.cpp
and then in form folder has
1: dialog.cpp.
when i add the new class my_qlabel with base class QLabel, type information: QWidget after creating this the setPixmap does not work. its show error the error is that "in constructor 'Dialog::Dialog(QWidget*)' class my_qlabel has no member named setPixmap" please solve this error or reffer me any site or other sources.
**my_qlabel.cpp**
#include "my_qlabel.h"
#include <QPixmap>
my_qlabel::my_qlabel(QWidget *parent) : QWidget(parent)
{
}
void my_qlabel::mouseMoveEvent(QMouseEvent *)
{
emit Mouse_Pos();
}
void my_qlabel::mousePressEvent(QMouseEvent *)
{
emit Mouse_Pressed();
}
void my_qlabel::leaveEvent(QEvent *)
{
emit Mouse_Left();
}
**main.cpp**
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
**dialog.cpp**
#include "dialog.h"
#include "ui_dialog.h"
#include "my_qlabel.h"
#include <QPixmap>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QPixmap pix("E:/osld/andgate.png");
ui->lblMouse->setPixmap(pix);
connect(ui->lblMouse, SIGNAL(Mouse_Pressed()), this, SLOT(Mouse_Pressed()));
connect(ui->lblMouse, SIGNAL(Mouse_Pos()), this, SLOT(Mouse_current_pos()));
connect(ui->lblMouse, SIGNAL(Mouse_Left()), this, SLOT(Mouse_left()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::Mouse_current_pos()
{
ui->lblMouse_Current_Event->setText("Mouse Moving");
}
void Dialog::Mouse_Pressed()
{
ui->lblMouse_Current_Event->setText("Mouse Pressed");
}
void Dialog::Mouse_left()
{
ui->lblMouse_Current_Event->setText("Mouse Left");
}
**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 slots:
void Mouse_current_pos();
void Mouse_Pressed();
void Mouse_left();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
**my_qlabel.h**
#ifndef MY_QLABEL_H
#define MY_QLABEL_H
#include <QWidget>
#include <QMouseEvent>
#include <QEvent>
#include <QDebug>
#include <QPixmap>
class my_qlabel : public QWidget
{
Q_OBJECT
public:
explicit my_qlabel(QWidget *parent = 0);
void mouseMoveEvent(QMouseEvent *ev);
void mousePressEvent(QMouseEvent *ev);
void leaveEvent(QEvent *);
signals:
void Mouse_Pressed();
void Mouse_Pos();
void Mouse_Left();
public slots:
};
#endif // MY_QLABEL_H
I have this two class in c++
GUI.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() {
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w.show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
// TODO Auto-generated destructor stub
}
GUI.h
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mainwindow.h"
#ifndef GUI_H_
#define GUI_H_
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow w;
};
#endif
But when i run this program i have the error:
QWidget: Must construct a QApplication before a QPaintDevice
How can I declare MainWindow w in gui.h in such a way that I don't receive this error
You can't (well, you can, but you shouldn't). The MainWindon declaration is right where it should be. The problem is that you attempt to create a GUI object before you create the QApplication.
Why not create the QApplication where you create the GUI object, just before it?
I would have made w a pointer used a forward declaration for MainWindow and removed all the includes (including the 2 includes for mainwindow.h) from GUI.h. Then like the answer from Sebastian says construct the QApplication first.
AL_GUI.h
#ifndef GUI_H_
#define GUI_H_
class MainWindow;
class GUI {
public:
GUI();
virtual ~GUI();
void startGUI();
void notifyAlert();
private:
MainWindow* w;
};
gui.cpp
#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"
GUI::GUI() : w(NULL)
{
}
void GUI::startGUI(){
int c=1;
char *array[10];
char** v = &array[0];
QApplication qa(c,v);
w = new MainWindow;
w->show();
qa.exec();
}
void GUI::notifyAlert(){
}
GUI::~GUI() {
delete w;
}
I would like to know how to call the function Do_Download() from the SocketTest class outisde the main() function.
The first cTest.Do_Download() does work, but when I call the test() function, the csTest.Do_Download() does not work.
So it looks like I can only acces SocketTest from inside the main() function, and not from any other function.
Does somebody know how this can be solved?
Thanks!
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
SocketTest cTest;
cTest.Do_Download();
return a.exec();
}
void test()
{
qDebug() << "test main functie";
SocketTest csTest;
csTest.Do_Download();
}
SocketTest.h:
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QTcpSocket>
#include <QDebug>
#include <QHttp>
#include <QFile>
#include <QString>
class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = 0);
void Do_Download();
signals:
public slots:
void stateChanged ( int state );
void responseHeaderReceived ( const QHttpResponseHeader & resp );
void requestFinished ( int id, bool error );
private:
QTcpSocket *socket;
QHttp *http;
QHttp *http2;
};
#endif // SOCKETTEST_H
If your DoDownload function is doing anything asynchronously (likely, when dealing with the Qt networking classes), the SocketTest you are creating in test() is being destroyed before it can act on any return value.
It works in main() because the event loop starts and the SocketTest instance hangs around.
I have a QWidget-derived class. In the constructor I say:
setPalette(QPalette(QColor(250,250,200)));
setAutoFillBackground(true);
Then in my widget's paintEvent() I say:
QPainter painter(this);
painter.drawRect(1,2,3,4);
There is also an updateNow() slot...which just calls update(). How can I make sure my palette doesn't get erased after that update call?
I don't have any problems with the following:
#include <QApplication>
#include <QWidget>
#include <QPalette>
#include <QPaintEvent>
#include <QPainter>
class Test : public QWidget
{
public:
Test()
{
setPalette(QPalette(QColor(250, 250, 200)));
setAutoFillBackground(true);
}
protected:
virtual void paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.drawRect(10, 20, 30, 40);
}
virtual void mousePressEvent(QMouseEvent*)
{
update();
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Test myTest;
myTest.show();
return app.exec();
}
The rectangle draws, and stays after I click, which triggers update. What are you seeing?