PDA

View Full Version : Subclass Problem



rleojoseph
21st January 2011, 08:18
hi, i am trying to subclass QMouseEvent into myclass.

I got some error while compiling the program over here,

Link::Link(QWidget *parent) :
QMainWindow(parent), // if i add QMouseEvent here it is showing some error
ui(new Ui::Link)

it is saying that no matching function for call QMouseEvent::QMouseEvent().


Some help would be welcomed

Sven
21st January 2011, 08:24
Because you do not use the correct constructors.


QMouseEvent ( Type type, const QPoint & position, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )


QMouseEvent ( Type type, const QPoint & pos, const QPoint & globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )

There is no default constructor ;-) ( and there is no constructor with a QWidget * as parent)

rleojoseph
21st January 2011, 10:12
Fine. I was trying to make a mouse click event into a TextEdit. That is why i was trying to subclass mouseevent in my class.
Before that i got a doubt.
Am using the QPoint,mapFromGlobal to get the cursor position. But whenever i am clicking outside the textedit, i can get print the cursor postion(using qDebug), if i click inside the TextEdit, i dont get any postion????

Can you tell me why?? All the thing which am trying will work?

Stef
21st January 2011, 10:23
I will give you a minimal working example to get you started. I started a new project added a textEdit to the main form.

Then I added a new class (MyTextEdit) which subclasses from QTextEdit:

header file

#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H

#include <QTextEdit>

class MyTextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit MyTextEdit(QWidget *parent = 0);

void mousePressEvent(QMouseEvent *e);

signals:

public slots:

};

#endif // MYTEXTEDIT_H

implementation:

#include "mytextedit.h"
#include <QDebug>
#include <QMouseEvent>

MyTextEdit::MyTextEdit(QWidget *parent) :
QTextEdit(parent)
{
}

void MyTextEdit::mousePressEvent(QMouseEvent *e)
{
qDebug() << e->pos();

QTextEdit::mousePressEvent(e);
}

I then promoted the QTextEdit on the form to a MyTextEdit (right-click -> promote to)

And this works perfectly.

output:
QPoint(107,59)
QPoint(55,91)
QPoint(128,141)
QPoint(164,121)
QPoint(152,88)
QPoint(73,62)
QPoint(21,42)
QPoint(8,46)
QPoint(2,45)

Hope this helps.

rleojoseph
21st January 2011, 11:02
Ya. Fine , I can accept the way you are subclass the TextEdit into ur Class. But what about your QMainWindow ?? That is the Form ??



class Link : public QMainWindow
{
Q_OBJECT


public:
explicit Link(QWidget *parent = 0);
~Link();

So i have to add another subclasss along with the Link, Isn't it?

Stef
21st January 2011, 11:18
No, you don't need a form per se. You could also just include mytextedit.h in your main procedure and then create a new MyTextEdit object and show that.


#include <QtGui/QApplication>
#include "mytextedit.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

MyTextEdit* myTextEdit = new MyTextEdit;
myTextEdit->show();

return a.exec();
}

rleojoseph
21st January 2011, 11:28
Thanks a lot stef!!

rleojoseph
25th January 2011, 11:25
Do you know any Examples which has many subclasses????

rleojoseph
27th January 2011, 07:58
@Stef: The way you said about subclass is working. What if happens when we need to implement the MyTextEdit class into another class.

Its like implementing the MyTextEdit class into the newclass which contains the PushButton and a textEdit Widget in the Form.

Stef
27th January 2011, 14:06
From your comment what I think you are trying to achieve is a form which contains your mytextedit and a pushbutton.

What you need to do to get this working is adding a QTextEdit widget to your form (assuming you have a MainWindow with according ui file). You then rightclick on the added QTextEdit and click in the context menu on 'Promote to...' In the promoted class field you fill in 'MyTextEdit' and in the header field it should say 'mytextedit.h'.

Next you click 'add' and 'promote'. Now you have a form with your subclassed textedit on it. You can then add a QPushbutton to the form. And have some text setted in your textedit.

The according code:

main.cpp:

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mytextedit.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

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:
Ui::MainWindow *ui;

private slots:

private slots:
void on_pushButton_clicked();
};

#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()
{
ui->textEdit->setText("Hello world!");
}

mytextedit.h:

#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H

#include <QTextEdit>

class MyTextEdit : public QTextEdit
{
Q_OBJECT
public:
explicit MyTextEdit(QWidget *parent = 0);

void mousePressEvent(QMouseEvent *e);

private:
QString str;

signals:


public slots:

};

#endif // MYTEXTEDIT_H

mytextedit.cpp:

#include "mytextedit.h"
#include <QDebug>
#include <QMouseEvent>

MyTextEdit::MyTextEdit(QWidget *parent) :
QTextEdit(parent)
{
}

void MyTextEdit::mousePressEvent(QMouseEvent *e)
{
qDebug() << e->pos();

QTextEdit::mousePressEvent(e);
}

rleojoseph
3rd February 2011, 05:46
Is there any other way to inherit the MyTextEdit(which inherits QTextEdit) class to another class. Now am not using drag and drop TextEdit widget.
Lets say
Class A
Class B
Class C
Class MyTextEdit

Now i need to inherit the Class B,C,MyTextEdit into A, how is that possible?

FelixB
3rd February 2011, 08:03
that's called "multiple inheritance". and yes, that is possible in c++.


class A : public B, public C, public MyTextEdit

rleojoseph
3rd February 2011, 09:05
ya,i know that, but when i am trying to implement it in QT, it is showing some errors!!!!:confused:

FelixB
3rd February 2011, 09:09
ya,i know that, but when i am trying to implement it in QT, it is showing some errors!!!!:confused:

then you are doing something wrong.

Stef
3rd February 2011, 10:35
ya,i know that, but when i am trying to implement it in QT, it is showing some errors!!!!:confused:

What does your code look like, and what errors are you receiving?

rleojoseph
3rd February 2011, 13:40
@Stef; The ideas which you have given are working well. but when i tried to make use of multiple classes, i got some bugs.
I have classes like ,
class text : public QMainwindow,public MyTextEdit

The error which i am getting is on main.cpp, those errors are

request for member 'show' is ambiguos
candidates are :void QWidget:show()


The textedit widget which i am using here is not a drag and drop widget. Thats why i was trying to integrate them in the class text

FelixB
3rd February 2011, 14:22
I guess, you don't have a "show" in your class?

rleojoseph
3rd February 2011, 14:24
nope. its there. i have created a obj for the text class and i gave .show() .

FelixB
3rd February 2011, 14:37
nope. I don't ask if you call "show()". I want to know if your class has it's own "show()"-method. maybe you can post the declaration of your class...

SixDegrees
3rd February 2011, 14:47
Both classes you inherit from are QWidgets. If your calling one of their show() methods, or attempting to provide your own version, you have to disambiguate which one you intend to call/override.

Without seeing your code, it's impossible to say where the problem lies. Honestly, this looks like a bad solution to pretty much any problem. Perhaps you should spend some time learning basic C++ before diving straight into multiple inheritance, which brings a whole host of problems along with it, and certainly before you start using Qt.

rleojoseph
4th February 2011, 05:37
int main(int argc, char *argv[]){
QApplication a(argc, argv);
Text w;
w.show();
return a.exec();
}

This is where the error i am gettting from main.cpp.
I was creating a textEdit by code not in designer form ,like this
textEdit = new QTextEdit(this);
I just want to know how to promote the custom MyTextEdit(which inherits QTextEdit) class to texEdit area....

stampede
4th February 2011, 06:43
I was creating a textEdit by code not in designer form ,like this
It's not about how you create your widget, read again what SixDegrees written, compiler is confused, because you do something like this:

class A{
public:
void show(){
}
};

class B{
public:
void show(){
}
};


class C : public A, public B{
};

int main(){
//...
C c;
c.show(); //!< and what is supposed to be called here : A::show() or B::show() ?
}



I just want to know how to promote the custom MyTextEdit(which inherits QTextEdit) class to texEdit area....
Stef has already given you a nice tutorial on promoting widgets in designer. Which part of it is not clear ?