PDA

View Full Version : Qpainter Help



athulms
5th September 2011, 09:26
I have created a ui window(mainwindow.ui). then i have added 3 qframe(Frame1, Frame2,Frame3) on the ui. then i placed an background image on the frame2.
Then i have created a class frame on the mainwindow.h. I have created a paintEvent of Qframe on the header in frame class. My purpose is to create a Qframe on the class frame. Then place that frame into the frame2 of the mainwindow.ui. So that i can draw over the background image of frame2. i don want painting in the frame1 & frame3. I frame2 i have to draw over the background image.


I coded and when i run my project the mainwindow.ui & frame class will run in 2different window.

main.cpp


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

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




mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QtGui>
#include<QtCore>
#include<QtDebug>
#include<QFrame>

namespace Ui {
class MainWindow;
class Frame;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
};
class Frame : public QFrame{
Q_OBJECT
public:
Frame( QWidget * parent = NULL ) : QFrame(parent){

}
protected:
void paintEvent( QPaintEvent * event );
};

#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 Frame::paintEvent( QPaintEvent * event )
{
QFrame::paintEvent(event);
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setCapStyle(Qt::RoundCap);
linepen.setWidth(25);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setRenderHint(QPainter::SmoothPixmapTransf orm,true);
painter.setPen(linepen);
painter.drawLine(100,100,300,300);

}

high_flyer
6th September 2011, 12:50
I coded and when i run my project the mainwindow.ui & frame class will run in 2different window.
That is correct, since your code is creating to top level widgets (windows).


#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show(); //<--- here you open the main window.
Frame m;
m.show(); //<--- her you open m as a top level window.
return a.exec();
}

If you want to have one widget to be "in" another, you have to make it a child widget.

athulms
12th September 2011, 06:16
how can i make my qframe as child widget

high_flyer
12th September 2011, 09:03
Read the basics of C++ specifically what is a member variable.
You can do it also as a non member, by giving the parent at construction time.
But you better start with C++ basics first.