PDA

View Full Version : Draw Line :confused:



sabeesh
23rd July 2007, 12:33
Hi,
I am new in QT. I do some small programes in QT.4.2.2. Now I try to
create a small program, which one draw a line in a particular widget
( label ). I create a form in QT4.2.2 and place three labels in that form named as Label1,
Label2 and Label3 and a pushButton.I need to run the program like this,
when i click on the pushbutton, then a line draw in a label3. How can i
do this?. I create two file like this, Please help me,
Sabeesh C.S
:confused:

testdraw.h
************************


#ifndef MYQTAPP_H
#define MYQTAPP_H
#include <QWidget>
#include "ui_testdraw.h"

class myQtApp : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
myQtApp(QWidget *parent = 0);
public slots:
void draw();
protected:
};
#endif


--------------------------------------------
testdraw.cpp
*************************


#include <QtGui>
#include <qpainter.h>
#include "testdraw.h"
myQtApp::myQtApp(QWidget *parent)
{
setupUi(this);
connect( pushButton, SIGNAL( clicked() ), this, SLOT( draw() ) );
}


void QLabel::paintEvent( QPaintEvent * ) {
QPainter painter( this );
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 40));
QLineF line(10.0, 10.0, 10.0, 120.0);
painter.drawLine(line);
}


void myQtApp::draw() {
qDebug("Hello");
}

------------------------------------

jacek
24th July 2007, 01:40
If you want to draw on a label, you have to create a subclass and use that subclass instead.

anafor2004
24th January 2008, 08:43
How can we do this subclassing? Can you give me an instance and example?

ashukla
24th January 2008, 08:55
How can we do this subclassing? Can you give me an instance and example?

#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>

class MyLabel : public QLabel
{
Q_OBJECT

public:
MyLabel(QWidget *parent = 0);


};
#endif
This is a prototype for your QLabel. Then you reimplement & add some functions according to your need.

anafor2004
24th January 2008, 09:01
Thanks for your reply but i wonder that how can we connect this paint event with my label on my form ,

Do I have to use this?

class drawLabel : public QLabel,public Ui::Label

ashukla
24th January 2008, 09:38
Thanks for your reply but i wonder that how can we connect this paint event with my label on my form ,

Do I have to use this?

class drawLabel : public QLabel,public Ui::Label
No, Because It cause a problem of inheriting two QObject. you can put
virtual void paintEvent ( QPaintEvent * event ) in your MyLabel class definition and then reimplement It in corresponding .cpp file.

anafor2004
24th January 2008, 10:03
This is what I did, but it didnt work.Why? Can you check it ?

cizim.h


#ifndef CIZIM_H
#define CIZIM_H

#include <QtGui/QMainWindow>
#include <QPainter>
#include "ui_cizim.h"

class cizim : public QMainWindow
{
Q_OBJECT

public:
cizim(QWidget *parent = 0);
~cizim();

private:
Ui::cizimClass ui;

protected:


public slots:
void draw();

};

#endif // CIZIM_H



cizim.cpp

#include "cizim.h"

cizim::cizim(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QObject::connect( ui.pushButton, SIGNAL( clicked() ), ui.label, SLOT( draw() ) );


}

cizim::~cizim()
{

}

void cizim::draw()
{
QPainter painter(ui.label);
painter.setPen(Qt::blue);
painter.drawLine(0,0,200,200);
}


ui_cizim.h

/************************************************** ******************************
** Form generated from reading ui file 'cizim.ui'
**
** Created: Thu 24. Jan 11:34:02 2008
** by: Qt User Interface Compiler version 4.3.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
************************************************** ******************************/

#ifndef UI_CIZIM_H
#define UI_CIZIM_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

class Ui_cizimClass
{
public:
QWidget *centralwidget;
QPushButton *pushButton;
QLabel *label;
QMenuBar *menubar;
QStatusBar *statusbar;

void setupUi(QMainWindow *cizimClass)
{
if (cizimClass->objectName().isEmpty())
cizimClass->setObjectName(QString::fromUtf8("cizimClass"));
cizimClass->resize(800, 600);
centralwidget = new QWidget(cizimClass);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(90, 180, 121, 71));
label = new QLabel(centralwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(270, 70, 351, 331));
cizimClass->setCentralWidget(centralwidget);
menubar = new QMenuBar(cizimClass);
menubar->setObjectName(QString::fromUtf8("menubar"));
menubar->setGeometry(QRect(0, 0, 800, 19));
cizimClass->setMenuBar(menubar);
statusbar = new QStatusBar(cizimClass);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
cizimClass->setStatusBar(statusbar);

retranslateUi(cizimClass);

QMetaObject::connectSlotsByName(cizimClass);
} // setupUi

void retranslateUi(QMainWindow *cizimClass)
{
cizimClass->setWindowTitle(QApplication::translate("cizimClass", "MainWindow", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("cizimClass", "PushButton", 0, QApplication::UnicodeUTF8));
label->setText(QString());
Q_UNUSED(cizimClass);
} // retranslateUi

};

namespace Ui {
class cizimClass: public Ui_cizimClass {};
} // namespace Ui

#endif // UI_CIZIM_H

jpn
24th January 2008, 10:48
void cizim::draw()
{
QPainter painter(ui.label);
painter.setPen(Qt::blue);
painter.drawLine(0,0,200,200);
}


You can't paint on widgets like this in Qt 4. Every widget paints itself in paintEvent(). No widget can paint on other widget and all painting happens during paintEvent().

ashukla
24th January 2008, 10:49
First you implement the class like this;
//mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *parent = 0);
virtual void paintEvent ( QPaintEvent * event );
};
#endif
and then write its corresponding mylabel.cpp. and its paintEvent you draw the line. After that you can use this label class in your constructor after setupUi(this). or use


#include "mylabel.h"
class Ui_cizimClass
{
public:
....
....
MyLabel *label;//QLabel *label;
...
...
You can attach code by the attachments tool.

anafor2004
24th January 2008, 11:26
Hi I tried to do this but i think , still make some mistake can you check please. Sorry for that I am a new beginner!!!

ui_cizim.h (I changed QLabel as MyLabel)

#include <QtGui/QButtonGroup>
#include "MyLabel.h"
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QStatusBar>
#include <QtGui/QWidget>

class Ui_cizimClass
{
public:
QWidget *centralwidget;
QPushButton *pushButton;
MyLabel *label;
QStatusBar *statusbar;

void setupUi(QMainWindow *cizimClass)
{
if (cizimClass->objectName().isEmpty())
cizimClass->setObjectName(QString::fromUtf8("cizimClass"));
cizimClass->resize(800, 600);
centralwidget = new QWidget(cizimClass);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
pushButton = new QPushButton(centralwidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(90, 180, 121, 71));
label = new MyLabel(centralwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(270, 70, 401, 331));
cizimClass->setCentralWidget(centralwidget);
statusbar = new QStatusBar(cizimClass);
statusbar->setObjectName(QString::fromUtf8("statusbar"));
cizimClass->setStatusBar(statusbar);

retranslateUi(cizimClass);

QMetaObject::connectSlotsByName(cizimClass);
} // setupUi

void retranslateUi(QMainWindow *cizimClass)
{
cizimClass->setWindowTitle(QApplication::translate("cizimClass", "MainWindow", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("cizimClass", "PushButton", 0, QApplication::UnicodeUTF8));
label->setText(QString());
Q_UNUSED(cizimClass);
} // retranslateUi

};

namespace Ui {
class cizimClass: public Ui_cizimClass {};
} // namespace Ui

#endif // UI_CIZIM_H


MyLabel.h

#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
#include<QPainter>

class MyLabel : public QLabel
{
Q_OBJECT
public:
MyLabel(QWidget *);
virtual void paintEvent ( QPaintEvent * event )
{
QPainter painter(this);
painter.setPen(Qt::blue);
painter.drawLine(0,0,100,100);



}
};
#endif /*SON_H_*/

MyLabel.cpp

#include "MyLabel.h"
MyLabel::MyLabel(QWidget *)
{


}


Cizim.h

#ifndef CIZIM_H
#define CIZIM_H

#include <QtGui/QMainWindow>
#include <QPainter>
#include "ui_cizim.h"
//#include "son.h"
class cizim : public QMainWindow,public QLabel
{
Q_OBJECT

public:
cizim(QWidget *parent = 0);
~cizim();

private:
Ui::cizimClass ui;

protected:


public slots:
void draw();

};

#endif // CIZIM_H

Cizim.cpp

#include "cizim.h"
cizim::cizim(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect( ui.pushButton, SIGNAL( clicked() ),this,SLOT( draw() ) );
}

cizim::~cizim()
{

}

void cizim::draw()
{

ui.label->repaint();
//this->close();

}
main.cpp

#include "cizim.h"
#include "MyLabel.h"
#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cizim w;
w.show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}


Thank you for your interest.