Page 1 of 3 123 LastLast
Results 1 to 20 of 46

Thread: Qpainter function on a QFrame problem

  1. #1
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Qpainter function on a QFrame problem

    i am doing a total re-write of a Qt3 program in Qt4
    I am using Kubuntu 7.10 distro.
    I have a QFrame area "frameA" that I want to use for my drawing (QpaintEvent ).
    My Code:
    Qt Code:
    1. void BaseForm::paintEvent ( QPaintEvent * event )
    2. {
    3. QString k, h;
    4. double angle;
    5. int n, xs, xe, ys, ye, xc, yc;
    6. bool ok;
    7. QPainter p ( this );
    8.  
    9. QPen pen;
    10. QFont f;
    11.  
    12. if ( paintSupportLines.count () > 0 )
    13. {
    14. f.setFamily ( "Courier" );
    15. f.setPointSize ( dwgScaleFactor );
    16. p.setFont ( f );
    17. pen.setBrush ( Qt::blue );
    18. p.setPen ( pen );
    19. p.setViewport ( viewportRect );
    20. p.setWindow ( windowRect );
    21.  
    22. if ( clearWindow == "erase" ) { p.eraseRect ( windowRect ); clearWindow = "cancel erase"; }
    23. else
    24. {
    25. for ( n = 0; n < paintSupportLines.count(); ++n )
    26. {
    27. p.setClipRect ( clippingRect );
    28. k = paintSupportLines[n];
    29. tempList = k.split ( "," );
    30. xs = tempList[0].toInt ( &ok );
    31. ys = tempList[1].toInt ( &ok );
    32. xe = tempList[2].toInt ( &ok );
    33. ye = tempList[3].toInt ( &ok );
    34. p.drawLine ( xs, ys, xe, ye );
    To copy to clipboard, switch view to plain text mode 
    etc. I can single step thru the program, all values seem correct.,
    Since I want to have my graphics in the "frameA" area of the screen I thought maybe I should change line #7 to "QPainter p( frameA )". but this did not help.

    The code samples in "The Book of Qt 4" uses separate classes for their "QPaintEvent"s
    Do I have to do that?

    I would appreciate some help as I am at a total loss.

    thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qpainter function on a QFrame problem

    If you want to draw on frameA, you must reimplement its paintEvent() instead of some other widget's paintEvent(), here BaseForm. In other words, you must implement paintEvent() of the widget you draw on.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Thanks,
    I am not sure on how to "reimplement its paintEvent()
    I have
    Qt Code:
    1. void BaseForm::slotSetPaintItems ( QStringList list )
    2. {
    3. int n ;
    4.  
    5. for ( n = 0; n < list.count(); ++n )
    6. {
    7. tempList = list[n].split ( "|" ); /// split the list into types of stringLists
    8. k = tempList[0]; /// get the type of element to draw
    9. if(k == "erase") clearWindow = "erase";
    10. else
    11. {
    12. tempList.removeFirst(); /// remove the element with the code
    13. if(k == "supports") paintSupportLines = tempList; ///
    14. else if (k == "columns") paintColumns = tempList;
    15. else if(k == "erase") clearWindow = "true";
    16. }
    17. }
    18. BaseForm::update(); /// now update it.
    19. }
    To copy to clipboard, switch view to plain text mode 
    Is this where I would do it if I knew How??
    My original "frameA"
    Qt Code:
    1. frameA = new QFrame ( this );
    2. frameA->setFrameShape ( QFrame::StyledPanel );
    3. frameA->setAutoFillBackground ( true );
    4. frameA->setPalette ( QColor ( 255, 255, 235 ) );
    5. frameA->setFrameShadow ( QFrame::Raised );
    To copy to clipboard, switch view to plain text mode 
    Again I would appreciate your help
    pete

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qpainter function on a QFrame problem

    Just like BaseForm does it. Subclass QFrame and reimplement paintEvent(). Again, BaseForm cannot paint on QFrame when BaseForm happens to receive a paint event. QFrame must do painting itself whenever it receives a paint event.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Thanks again, but I am a very old tired frustrated engineer. Please give me the code to do this as everytime I try the program locks up.
    thanks

    Hold up I think I found what you are talking about. I will let you know how I make out.
    Last edited by impeteperry; 22nd January 2008 at 20:34.

  6. #6
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    I'm back.
    I have not been able to make any headway. I have loaded examples from "Programming with Qt4", "The Book of Qt4". I have added all kinds of widgets to my QFrame frameA.
    I have QPainter p( to all kinds of stuff ) .I can change size, add colors etc. But NO LINES!!
    In Qt3 I had no trouble, even rendering solids, but not in Qt4.

    I know it must be a very simple thing, but if you don't know it.......

    I would love to see a simple working example that shows exactly what is necessary for drawing a line on a QFrame widget.

    thanks

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qpainter function on a QFrame problem

    Quote Originally Posted by impeteperry View Post
    I have QPainter p( to all kinds of stuff )
    You just can't paint on whatever widget you like to. You can't write
    Qt Code:
    1. QPainter p(someOtherWidgetSomewhere);
    To copy to clipboard, switch view to plain text mode 
    but you make the some other widget somewhere do
    Qt Code:
    1. QPainter p(this);
    To copy to clipboard, switch view to plain text mode 
    in its own paintEvent().

    Quote Originally Posted by impeteperry View Post
    I would love to see a simple working example that shows exactly what is necessary for drawing a line on a QFrame widget.
    As I said, you must subclass QFrame and reimplement paintEvent()
    Qt Code:
    1. class MyCustomFrame : public QFrame
    2. {
    3. ...
    4. protected:
    5. void paintEvent(QPaintEvent* event); // <-- a reimplemented method
    6. };
    7.  
    8. MyCustomFrame::paintEvent(QPaintEvent* event)
    9. {
    10. QPainter painter(this); // <-- look, i can paint on myself
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Hi, I have
    Qt Code:
    1. #ifndef MYCUSTOMFRAME_H
    2. #define MYCUSTOMFRAME_H
    3.  
    4. #include "baseform.h"
    5. #include <qframe.h>
    6. #include <qpainter.h>
    7.  
    8.  
    9. class MyCustomFrame : public QFrame
    10. {
    11. Q_OBJECT
    12.  
    13. protected:
    14. void paintEvent(QPaintEvent* event);
    15. };
    16.  
    17. void MyCustomFrame::paintEvent(QPaintEvent* event)
    18. {
    19. QPainter painter(this);
    20. painter.drawLine(0, 0, 200, 150);
    21. }
    22.  
    23. #endif // MYCUSTOMFRAME_H
    To copy to clipboard, switch view to plain text mode 
    and it compiles with out errors or warnings.
    I have 2 quentions:
    1. How do I access it from the body of my program as I was required to remove "#include "mycustomframe.h" from "baseform.cpp"?

    2. I have a class "drawingfunctions.h" where I would like to do the painting. If I read you correctly, I need to change the class name from
    Qt Code:
    1. class DrawingFunctions : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. class DrawingFunctions : public QFrame
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. DrawingFunctions( QWidget *parent = 0 );
    To copy to clipboard, switch view to plain text mode 
    and i think the "parent = 0" is wrong.
    And what do I need to change here?
    Qt Code:
    1. DrawingFunctions::DrawingFunctions( QWidget *parent ) : QWidget(parent)
    To copy to clipboard, switch view to plain text mode 
    As you can tell, i have no clear understanding of whats going on. I am able to use QTextEdit widgets on my base QFrame, but not the QPainter.
    (These "uncontrolled" events of Qt4 are driving me up a wall.)

    I really appreciate your patience with me.

  9. #9
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    I never have had as much trouble with anything I can remember.
    As you suggested I setup a new class "MyDrawingFrame" and implemented it as best as I could. It compiles without any warnings
    Qt Code:
    1. #ifndef MYDRAWINGFRAME_H
    2. #define MYDRAWINGFRAME_H
    3.  
    4. #include "baseform.h"
    5. #include <qframe.h>
    6. #include <qpainter.h>
    7.  
    8. class MyDrawingFrame : public QFrame
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13.  
    14. public slots:
    15.  
    16. protected:
    17. void paintEvent(QPaintEvent * event);
    18.  
    19. protected slots:
    20.  
    21. private:
    22. QStringList tempList;
    23. QStringList paintSupportLines;
    24. QStringList paintColumns;
    25. QString clearWindow;
    26.  
    27. QRect viewportRect;
    28. QRect windowRect;
    29. QRect clippingRect;
    30. double pi;
    31. int activeLevel;
    32. int functionNumber;
    33. int dwgScaleFactor;
    34. int count;
    35. int num;
    36. int n;
    37. int o1;
    38.  
    39. bool ok;
    40.  
    41. private slots:
    42.  
    43. signals:
    44.  
    45. };
    46.  
    47. void MyDrawingFrame::paintEvent(QPaintEvent * event)
    48. {
    49. QString k, h;
    50. double angle;
    51. int n, xs, xe, ys, ye, xc, yc;
    52. bool ok;
    53.  
    54. QPainter p(this);
    55. QPen pen;
    56. QFont f;
    57.  
    58. if ( paintSupportLines.count () > 0 )
    59. {
    60. f.setFamily ( "Courier" );
    61. f.setPointSize ( dwgScaleFactor );
    62. p.setFont ( f );
    63. pen.setBrush ( Qt::blue );
    64. p.setPen ( pen );
    65. p.setViewport ( viewportRect );
    66. p.setWindow ( windowRect );
    67.  
    68. if ( clearWindow == "erase" ) { p.eraseRect ( windowRect ); clearWindow = "cancel erase"; }
    69. else
    70. {
    71. for ( n = 0; n < paintSupportLines.count(); ++n )
    72. {
    73. //teErrorMessages->setText("F5 key clicked");
    74. p.setClipRect ( clippingRect );
    75. k = paintSupportLines[n];
    76. tempList = k.split ( "," );
    77. xs = tempList[0].toInt ( &ok );
    78. ys = tempList[1].toInt ( &ok );
    79. xe = tempList[2].toInt ( &ok );
    80. ye = tempList[3].toInt ( &ok );
    81. p.drawLine ( 10, 10 * n, 200, 200 );
    82. p.drawLine ( xs, ys, xe, ye );
    83. /// start of labeling.
    84. if ( tempList[4] == "true" )
    85. {
    86. p.setClipRect ( windowRect );
    87. xc = ( xs + xe ) / 2;
    88. yc = ( ys + ye ) / 2;
    89. if ( xs == xe ) angle = pi/2.0;
    90. else if ( ys == ye ) angle = 0.0;
    91. else
    92. {
    93. // angle = atan((double)(ye-ys)/(xe-xs)); /// Why did Qt 4 drop trig functions????
    94. angle = ( double ) ( ye-ys ) / ( xe-xs ); /// hope this works!!!
    95. angle /= 2.0 * pi ;
    96. }
    97. angle = ( angle * 180.0 ) / pi;
    98. p.save();
    99. p.translate ( xc, yc );
    100. p.rotate ( angle ); //teErrorMessages->setText("F5 key clicked");
    101. p.scale ( 1, -1 );
    102. k = "S" + h.setNum ( n + 1 );
    103. p.drawText ( 0, 0, k );
    104. p.restore();
    105. }
    106. }
    107. }
    108. if ( paintColumns.count() > 0 )
    109. {
    110. p.setViewport ( viewportRect );
    111. p.setWindow ( windowRect );
    112. p.setClipRect ( windowRect );
    113. for ( n = 0; n < paintColumns.count(); ++n )
    114. {
    115. k = paintColumns[n];
    116. tempList = k.split ( "," );
    117. xs = tempList[0].toInt ( &ok );
    118. ys = tempList[1].toInt ( &ok );
    119. xe = tempList[2].toInt ( &ok );
    120. ye = tempList[3].toInt ( &ok );
    121. angle = tempList[4].toInt ( &ok );
    122. p.save();
    123. p.translate ( xs, ys );
    124. p.rotate ( angle );
    125. p.fillRect ( -xe/2, -ye/2, xe, ye, Qt::black );
    126. if ( tempList[5] == "true" )
    127. {
    128. f.setFamily ( "Courier" );
    129. f.setPointSize ( ( int ) ( .8 * dwgScaleFactor ) );
    130. p.setFont ( f );
    131. pen.setBrush ( Qt::black );
    132. p.setPen ( pen );
    133. p.rotate ( -angle + 25 );
    134. p.scale ( 1, -1 );
    135. k = "---C" + h.setNum ( n + 1 );
    136. p.drawText ( 0, 0, k );
    137. }
    138. p.restore();
    139. }
    140. }
    141. }
    142. }
    143. #endif // MYDRAWINGFRAME_H
    To copy to clipboard, switch view to plain text mode 
    Great, but how do I access it to set the values of the variables and activate the paint event itself??. If I "#include "mydrawingframe.h" in my "baseframe.cpp" or anyplace else for that matter I get an
    /home/pete/Desktop/pm-straight-C/pm/mydrawingframe.h:65: multiple definition of `MyDrawingFrame:aintEvent(QPaintEvent*)'
    baseform.o:/home/pete/Desktop/pm-straight-C/pm/mydrawingframe.h:65: first defined here
    collect2: ld returned 1 exit status
    make: *** [pm] Error 1
    I have also tried setting up a "mydrawingframe.cpp" but I could not get a "MyDrawingFrame::MydrawingFrame( Q??? *parent ) : Q???
    t(parent) that did not give an error when compiling.
    Sorry to be so long winded, but I am desperate. What in H--- am I missing??? All I want to do is draw some lines of a QFrame widget!!!!!

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qpainter function on a QFrame problem

    Have you seen this thread?

    http://www.qtcentre.org/forum/f-qt-d...bel-11504.html

    Especially take a look at how the line is drawn depending on the size of the widget.

  11. The following user says thank you to wysota for this useful post:

    impeteperry (27th January 2008)

  12. #11
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Thanks Mysota It was a great help.My big problem I forgot the line under the "public;" when defining the class. What ever you do, don't get old (I'm 81).

    Now all I have to do is figure out how to set the size of the frame, but I"m going to bed it's 12:30.

    Thanks again.

  13. #12
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Thanks Mysota It was a great help.My big problem
    Please mention the name correctly as Wysota, Qt Guru.
    Last edited by wysota; 28th January 2008 at 10:19. Reason: Please use proper tags - [quote] instead of [code] :P
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  14. #13
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Wysots, Qi Guru, I'm sorry about the name. It was late and I don't see to well.
    I have made some progress. I can draw some lines, so I am back at setting up my scaling functions.
    I do have a question. In the distant past I wrote to a "pixmap". is there any advantage to that now with the paintEvent functionallity?
    Thanks

  15. #14
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    Hi
    I now have my painter working on my laptop whereI compiled the program.
    It also runs on my desk computer as long as I don't re-compile it there. If I re-compile the same source code there it locks up the computer on the first "p.drawText" command. I have single stepped the program on both computers. Everything is the same. There is no problem with the "p.drawLine" command. If I option out the single p.drawText(x, y, "text"); line 16, the program runs fine.
    Qt Code:
    1. p.drawLine ( xs, ys, xe, ye );
    2. /// start of labeling.
    3. if ( tempList[4] == "true" )
    4. {
    5. xc = ( xs + xe ) / 2;
    6. yc = ( ys + ye ) / 2;
    7. if ( xs == xe ) angle = pi/2.0;
    8. else if ( ys == ye ) angle = 0.0;
    9. else angle = atan((double)(ye - ys) / (double)(xe - xs));
    10. angle = ( angle * 180.0 ) / pi;
    11. p.save();
    12. p.translate ( xc, yc );
    13. p.rotate ( angle );
    14. p.scale ( 1, -1 );
    15. k = "S" + h.setNum ( n + 1 );
    16. p.drawText ( 0, 0, k );
    17. p.restore();
    18. }
    To copy to clipboard, switch view to plain text mode 
    I am using the same Kubuntu 7.10 on each computer.
    I am sorry to be such a pest.
    Thanks for help.

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qpainter function on a QFrame problem

    What exactly is the widget supposed to display?

  17. #16
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    I have a bunch of lines and i label them with their number. the text I want to display is "S1" See attachment. These are "Grid" lines are used for locating building columns and other elements.
    Except for the screen size the data is identical on the two computers. The lines draw correctly so the "viewWindow" and "viewPort" are set correctly. If I take the compiled program from the second machine, it runes fine on the firsrt machine.

    In essense, the program compiled on the first computer runs fine on both computers
    The program compiled on the second computer locks it up when run, but runs fine on the first computer.
    Attached Images Attached Images

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qpainter function on a QFrame problem

    Why don't you use QGraphicsView to make the visualization?

  19. #18
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    QGraphicsView was add in Qt4.2. I was unaware of its existance
    Thanks, I will take a look at it.

  20. #19
    Join Date
    Jan 2006
    Location
    Riverside, Rhode Island, USA
    Posts
    245
    Thanks
    52
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qpainter function on a QFrame problem

    I took a quick look at your QGraphicView and found
    By default, the items are drawn onto the viewport by using a regular QPainter, and using default render hints.
    What would be the advantage using this when I already have the geometry and am already using the "painter" to draw items onto the "viewPort"?

  21. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qpainter function on a QFrame problem

    Ease of handling. I don't say you should take that approach, but that if you have problems with one mechanism, try using another.

  22. The following user says thank you to wysota for this useful post:

    impeteperry (1st February 2008)

Similar Threads

  1. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  3. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 14:48
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.