Results 1 to 20 of 46

Thread: Qpainter function on a QFrame problem

Hybrid View

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

    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!!!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    impeteperry (27th January 2008)

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

    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.

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

    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 11: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!

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

    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

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

    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.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Qpainter function on a QFrame problem

    What exactly is the widget supposed to display?

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

    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

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

    Default Re: Qpainter function on a QFrame problem

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

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

    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.

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

    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"?

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.

  14. 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, 13:57
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  3. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 15:48
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13: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
  •  
Qt is a trademark of The Qt Company.