PDA

View Full Version : Difficulty in drawing a line



rezas1000
29th July 2014, 06:41
Hello., I would like to give a one-line display. But what is not shown. Please help me.

#include <QApplication>
#include <QtWidgets>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPainter* painter=new QPainter;
painter->drawLine(100,100,200,200);
return app.exec();
}

Radek
29th July 2014, 07:13
Where do you want to paint?

rezas1000
29th July 2014, 07:22
Where do you want to paint?

For example, in QMainWindow.

Radek
29th July 2014, 08:12
Where is the main window? QApplication is not a window. It is only a control structure of your app. Therefore, create a main window, bind the painter to a widget of the main window and draw a line.

rezas1000
29th July 2014, 08:26
Thank you very much., Please do me the code. Because I know better.

Radek
29th July 2014, 09:01
A long story :) You can try this tutorial: http://www.zetcode.com/gui/qt4/ It contains simple demonstrations of discussed matter. First, learn creating a window, then paint a line on it. There are also several good books on Qt around:

Blanchette, Summerfield - C++ GUI Programming with Qt 4, Second Edition (suitable starting point)
Molkentin - The Book of Qt4 (suitable starting point)
Summerfield - Advanced Qt Programming (advanced, not a starting point)
Ezust, Ezust - An Introduction to Design Patterns in C++ with Qt (advanced, not a starting point)

rezas1000
29th July 2014, 10:07
I thank you very much. I've copied the code on that site. Error message sent. And did not compile., I've attached a file for you. Please check your. I will be very sorry.

Radek
29th July 2014, 15:35
Which errors have you got? I downloaded the files and made a project. It compiles, runs and shows the lines. I made the following updates:

(1) Rename "untitled.pro" to something else. "Untitled" might be a reserved file name. I renamed to "zz.pro".
(2) Remove "widgets" from zz.pro - I have Qt 4.8, "widgets" is for Qt 5.0 and higher versions. If you have Qt 5.0 (or newer), leave "widgets" in zz.pro .

rezas1000
31st August 2014, 12:51
Why the following program does not run properly?
thanks.

#ifndef E_H
#define E_H
#include <QWidget>
#include <QPaintEvent>
class paint:public QWidget
{public:
paint(QWidget* parent=0);
protected:
void paintevent(QPaintEvent*event);
};
#endif // E_H


#include <QtWidgets>
#include "paint.h"
paint::paint(QWidget *parent): QWidget(parent)
{


setWindowTitle("Paint");
resize(500, 500);}
void paint::paintevent(QPaintEvent *)
{
QPainter painter(this);
QPen pen (Qt::black, 2, Qt::SolidLine);

painter.setPen(pen);
painter.drawLine(100,100,100,100);
}


#include <QtWidgets>
#include "paint.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
paint pa;
pa.show();
return app.exec();
}


QT=widgets
SOURCES += \
main.cpp \
paint.cpp

HEADERS += \
paint.h

10604

anda_skoa
31st August 2014, 14:03
C++ is a case sensitive language. paintevent is not the same thing as paintEvent

Cheers,
_