Results 1 to 20 of 20

Thread: How I could Tell Qpainter where is his Area of Painting...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How I could Tell Qpainter where is his Area of Painting...

    I made a GUI with designer.
    I got a QWidget object called "areadibujo" that has and predetermined area.
    Now the problem is that I dont know how to tell "QPainter" that his Space is That one.

    Which is the way to tell it that?

    Could be QPainterPath? how i could implemented ? I got days within this.
    Am I doing something wrong? any ideas?

    No one?
    Last edited by j0rt4g4; 25th November 2008 at 20:34.

  2. #2
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    Is my question wrong formulated ? :S

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    What it is you want to do?
    ANY widget that you can see is painted by QPainter.
    If you want to change the way a particular widget is being drawn, you should overload the paintEvent() of that widget.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    I got a image like this:

    In the Center of this image there's a widget called "areadibujo",
    I want to paint ON this area, but I dont know how to do it.

    I want to create a code that read a file data
    (%f %f ) insert it on a array
    a[i] b[i]

    And use the array to do some math, and get the points on areadibujo but I guess I'm confused with parents.

    Well here's my code:
    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include "fractal.h"
    3. #include "renderarea.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. fractal *dialog = new fractal;
    9. //renderarea *dialog= new renderarea;
    10.  
    11. dialog->show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    fractal.h
    Qt Code:
    1. #ifndef __FRACTAL_H__
    2. #define __FRACTAL_H__
    3. #include "ui_fractals.h"
    4.  
    5.  
    6. class fractal : public QWidget, private Ui::Fractals
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. fractal(QWidget *parent = 0);
    12. void updateBrush();
    13.  
    14. public slots:
    15. void setOpenFileName();
    16. void QPaintEvent(QPainter *event);
    17. };
    18.  
    19.  
    20. #endif // __FRACTAL_H__
    To copy to clipboard, switch view to plain text mode 

    fractal.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QFile>
    3. #include <QGLWidget>
    4. #include <QtOpenGL>
    5. #include "fractal.h"
    6.  
    7. void tidyFile(QIODevice *inDevice, QIODevice *outDevice)
    8. {
    9. QTextStream in(inDevice);
    10. QTextStream out(outDevice);
    11.  
    12. int endlCount = 0;
    13. QChar ch;
    14.  
    15. while (!in.atEnd()) {
    16. in >> ch;
    17.  
    18. if (ch == '\n') {
    19. ++endlCount;
    20. }
    21. }
    22. // out << endl;
    23. }
    24.  
    25.  
    26. fractal::fractal(QWidget *parent){
    27. setupUi(this);
    28. connect( toolButton, SIGNAL( clicked() ), this, SLOT( setOpenFileName() ) );
    29. connect( boton_aceptar, SIGNAL( clicked() ), this, SLOT( QPaintEvent(QPainter *event) ) );
    30. }
    31.  
    32.  
    33. void fractal::setOpenFileName()
    34. {
    35. QFileDialog::Options options;
    36. options |=QFileDialog::ShowDirsOnly;
    37.  
    38.  
    39. QString selectedFilter;
    40. QString fileName = QFileDialog::getOpenFileName(this,
    41. tr("QFileDialog::getOpenFileName()"),
    42. lineEdit->text(),
    43. tr("Todos (*);;Data (*.txt)"),
    44. &selectedFilter,
    45. options);
    46. if (!fileName.isEmpty())
    47. lineEdit->setText(fileName);
    48.  
    49. QFile infile;
    50. QFile outfile;
    51. infile.open(stdin, QFile::ReadOnly);
    52. outfile.open(stdout, QFile::WriteOnly);
    53. tidyFile(&infile,&outfile);
    54. QTextStream in(&infile);
    55. QTextStream out(&outfile);
    56. int endlCount = 0;
    57. QChar ch;
    58. while (!in.atEnd()) {
    59. in >> ch;
    60. if (ch == '\n') {
    61. ++endlCount;
    62. }
    63. }
    64. }
    65.  
    66. void fractal::QPaintEvent(QPainter *event){
    67. QPainter painter(this);
    68. painter.setRenderHint(QPainter::Antialiasing, true);
    69. painter.setPen(QPen(Qt::black, 15, Qt::SolidLine, Qt::RoundCap,
    70. Qt::MiterJoin));
    71. painter.setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
    72. painter.drawPie(80, 80, 400, 240, 60 * 16, 270 * 16);
    73. }
    To copy to clipboard, switch view to plain text mode 
    I hope I wasn't wasting my time as I hope...
    Last edited by j0rt4g4; 26th November 2008 at 16:34.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    it is wrong
    Qt Code:
    1. void fractal::QPaintEvent(QPainter *event)
    To copy to clipboard, switch view to plain text mode 
    must be
    Qt Code:
    1. void fractal::paintEvent(QPaintEvent *event) {
    2. //draw something
    3. }
    To copy to clipboard, switch view to plain text mode 

    and then you must draw what you want
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    Ok I made Corrections and IT DRAWS (thank you very much)... But It is painting on the BIG Widget (mainwindow) and It is not ON the widget called (areadibujo).

    If you want to change the way a particular widget is being drawn, you should overload the paintEvent() of that widget
    Could you tell me how I do that in here ?
    Thank you very much

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    is this "areadibujo" independent? if yes then you have to override paintEvent of this widget.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    mmm I guess it is a son of "Fractals"


    Isnt it easier make it independient?, T.T because I dont know how to do it independient eighter... (:S what a noobie guy).... I'm trying .
    Last edited by j0rt4g4; 26th November 2008 at 17:08.

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    yep. so you can do next: install event filter for this widget and handle paint event and draw what you need, e.g.
    Qt Code:
    1. Test::Test(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. QVBoxLayout *vbl = new QVBoxLayout();
    5. QWidget *w = new QWidget;
    6. m_drawer = new QWidget();
    7. m_drawer->installEventFilter(this);//set event filter
    8.  
    9. vbl->addWidget(m_drawer);
    10. vbl->addWidget(new QWidget);
    11. w->setLayout(vbl);
    12.  
    13. setCentralWidget(w);
    14. }
    15.  
    16. bool Test::eventFilter(QObject *o, QEvent *e)
    17. {
    18. if (o == m_drawer && e->type() == QEvent::Paint) {//process paint event of needed widget
    19. QPainter painter(m_drawer);
    20. painter.setPen(Qt::blue);
    21. painter.setFont(QFont("Arial", 30));
    22. painter.drawText(m_drawer->rect(), Qt::AlignCenter, "Qt");
    23. return true;
    24. }
    25. return QMainWindow::eventFilter(o, e);
    26. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. The following 2 users say thank you to spirit for this useful post:

    j0rt4g4 (26th November 2008), tpf80 (22nd January 2009)

  11. #10
    Join Date
    Nov 2008
    Location
    Caracas - Venezuela
    Posts
    29
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    Like this?

    ui_Fractals.h:
    Qt Code:
    1. QT_BEGIN_NAMESPACE
    2.  
    3. class Ui_Fractals
    4. {
    5. public:
    6. QWidget *layoutWidget;
    7. QHBoxLayout *horizontalLayout_4;
    8. QVBoxLayout *verticalLayout_2;
    9. QHBoxLayout *horizontalLayout_3;
    10. QLabel *label_Archivo;
    11. QLineEdit *lineEdit;
    12. QToolButton *toolButton;
    13. QWidget *areadibujo;
    14. QVBoxLayout *verticalLayout;
    15. QHBoxLayout *horizontalLayout;
    16. QLabel *Iterations;
    17. QSpinBox *spinbox_ite;
    18. QSlider *iterator_hslider;
    19. QSpacerItem *verticalSpacer;
    20. QHBoxLayout *horizontalLayout_2;
    21. QPushButton *boton_aceptar;
    22. QPushButton *boton_salir;
    23.  
    24. void setupUi(QWidget *Fractals)
    25. {
    26. if (Fractals->objectName().isEmpty())
    27. Fractals->setObjectName(QString::fromUtf8("Fractals"));
    28. Fractals->resize(605, 408);
    29. layoutWidget = new QWidget(Fractals);
    30. layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
    31. layoutWidget->setGeometry(QRect(10, 10, 579, 382));
    32. horizontalLayout_4 = new QHBoxLayout(layoutWidget);
    33. horizontalLayout_4->setObjectName(QString::fromUtf8("horizontalLayout_4"));
    34. horizontalLayout_4->setContentsMargins(0, 0, 0, 0);
    35. verticalLayout_2 = new QVBoxLayout();
    36. verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
    37. horizontalLayout_3 = new QHBoxLayout();
    38. horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
    39. label_Archivo = new QLabel(layoutWidget);
    40. label_Archivo->setObjectName(QString::fromUtf8("label_Archivo"));
    41.  
    42. horizontalLayout_3->addWidget(label_Archivo);
    43.  
    44. lineEdit = new QLineEdit(layoutWidget);
    45. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    46. lineEdit->setEnabled(false);
    47. lineEdit->setMinimumSize(QSize(280, 0));
    48. lineEdit->setDragEnabled(false);
    49.  
    50. horizontalLayout_3->addWidget(lineEdit);
    51.  
    52. toolButton = new QToolButton(layoutWidget);
    53. toolButton->setObjectName(QString::fromUtf8("toolButton"));
    54. toolButton->setMinimumSize(QSize(25, 20));
    55. toolButton->setCheckable(false);
    56. toolButton->setPopupMode(QToolButton::DelayedPopup);
    57. toolButton->setArrowType(Qt::NoArrow);
    58.  
    59. horizontalLayout_3->addWidget(toolButton);
    60.  
    61.  
    62. verticalLayout_2->addLayout(horizontalLayout_3);
    63.  
    64. areadibujo = new QWidget(layoutWidget);
    65. areadibujo->setObjectName(QString::fromUtf8("areadibujo"));
    66. areadibujo->setMinimumSize(QSize(350, 350));
    67. areadibujo->setMaximumSize(QSize(500, 500));
    68. areadibujo->setAutoFillBackground(false);
    69. areadibujo->installEventFilter(this); ////////////////////////////here!:confused::confused:
    70.  
    71.  
    72. verticalLayout_2->addWidget(areadibujo);
    73.  
    74.  
    75. horizontalLayout_4->addLayout(verticalLayout_2);
    76.  
    77. verticalLayout = new QVBoxLayout();
    78. verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    79. horizontalLayout = new QHBoxLayout();
    80. horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    81. Iterations = new QLabel(layoutWidget);
    82. Iterations->setObjectName(QString::fromUtf8("Iterations"));
    83.  
    84. horizontalLayout->addWidget(Iterations);
    85.  
    86. spinbox_ite = new QSpinBox(layoutWidget);
    87. spinbox_ite->setObjectName(QString::fromUtf8("spinbox_ite"));
    88. spinbox_ite->setEnabled(true);
    89. spinbox_ite->setMinimumSize(QSize(30, 0));
    90. spinbox_ite->setMinimum(100000);
    91. spinbox_ite->setMaximum(1000000);
    92. spinbox_ite->setSingleStep(1000);
    93. spinbox_ite->setValue(100000);
    94.  
    95. horizontalLayout->addWidget(spinbox_ite);
    96.  
    97. iterator_hslider = new QSlider(layoutWidget);
    98. iterator_hslider->setObjectName(QString::fromUtf8("iterator_hslider"));
    99. iterator_hslider->setEnabled(true);
    100. iterator_hslider->setMouseTracking(true);
    101. iterator_hslider->setMinimum(100000);
    102. iterator_hslider->setMaximum(1000000);
    103. iterator_hslider->setSingleStep(1000);
    104. iterator_hslider->setPageStep(100000);
    105. iterator_hslider->setValue(100000);
    106. iterator_hslider->setTracking(true);
    107. iterator_hslider->setOrientation(Qt::Horizontal);
    108.  
    109. horizontalLayout->addWidget(iterator_hslider);
    110.  
    111.  
    112. verticalLayout->addLayout(horizontalLayout);
    113.  
    114. verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    115.  
    116. verticalLayout->addItem(verticalSpacer);
    117.  
    118. horizontalLayout_2 = new QHBoxLayout();
    119. horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
    120. boton_aceptar = new QPushButton(layoutWidget);
    121. boton_aceptar->setObjectName(QString::fromUtf8("boton_aceptar"));
    122.  
    123. horizontalLayout_2->addWidget(boton_aceptar);
    124.  
    125. boton_salir = new QPushButton(layoutWidget);
    126. boton_salir->setObjectName(QString::fromUtf8("boton_salir"));
    127.  
    128. horizontalLayout_2->addWidget(boton_salir);
    129.  
    130.  
    131. verticalLayout->addLayout(horizontalLayout_2);
    132.  
    133.  
    134. horizontalLayout_4->addLayout(verticalLayout);
    135.  
    136.  
    137. retranslateUi(Fractals);
    138. QObject::connect(boton_salir, SIGNAL(clicked()), Fractals, SLOT(close()));
    139. QObject::connect(spinbox_ite, SIGNAL(valueChanged(int)), iterator_hslider, SLOT(setValue(int)));
    140. QObject::connect(iterator_hslider, SIGNAL(valueChanged(int)), spinbox_ite, SLOT(setValue(int)));
    141.  
    142. QMetaObject::connectSlotsByName(Fractals);
    143. } // setupUi
    144.  
    145. void retranslateUi(QWidget *Fractals)
    146. {
    147. Fractals->setWindowTitle(QApplication::translate("Fractals", "Fractals", 0, QApplication::UnicodeUTF8));
    148. label_Archivo->setText(QApplication::translate("Fractals", "Archivo:", 0, QApplication::UnicodeUTF8));
    149. toolButton->setText(QApplication::translate("Fractals", "...", 0, QApplication::UnicodeUTF8));
    150. Iterations->setText(QApplication::translate("Fractals", "Iterations", 0, QApplication::UnicodeUTF8));
    151. boton_aceptar->setText(QApplication::translate("Fractals", "&Aceptar", 0, QApplication::UnicodeUTF8));
    152. boton_salir->setText(QApplication::translate("Fractals", "&Salir", 0, QApplication::UnicodeUTF8));
    153. Q_UNUSED(Fractals);
    154. } // retranslateUi
    155.  
    156. };
    157.  
    158. namespace Ui {
    159. class Fractals: public Ui_Fractals {};
    160. } // namespace Ui
    161.  
    162. QT_END_NAMESPACE
    163.  
    164.  
    165. bool areadibujo::eventFilter(QObject *o, QEvent *e) //////////and here:confused::confused:
    166. {
    167. if(o == areadibujo && e->type() == QEvent::Paint){
    168. QPainter painter(areadibujo);
    169. painter.setPen(Qt::blue);
    170. painter.setFont(QFont("Arial",30));
    171. painter.drawText(areadibujo->rect(), Qt::AlignCenter,"Probando");
    172. return true;
    173. }
    174. return Ui_Fractals::eventFilter(o,e);
    175. }
    176.  
    177. #endif // UI_FRACTALS_H
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How I could Tell Qpainter where is his Area of Painting...

    no, not in generated h-file. you should install filter in that widget for which you set up ui, i.e.
    Qt Code:
    1. fractal::fractal(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setupUi(this);
    5. areadibujo->installEventFilter(this);
    6. connect( toolButton, SIGNAL( clicked() ), this, SLOT( setOpenFileName() ) );
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Replies: 7
    Last Post: 20th March 2006, 20:03
  2. 2 Questions about QPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 22nd February 2006, 15:08
  3. Trouble with image painting (QPainter + QImage)
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2006, 20:25
  4. QRubberBand painting in the scroll area widget
    By SkripT in forum Qt Programming
    Replies: 7
    Last Post: 17th January 2006, 16:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.