Results 1 to 13 of 13

Thread: call drawing function out of paintGL

Threaded View

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

    Default Re: call drawing function out of paintGL

    Here is a simple example, adapt it to your needs. I'm not commenting it on purpose so that you have to put some effort in understanding it.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget {
    4. Q_OBJECT
    5. public:
    6. Widget(QWidget *parent = 0) : QWidget(parent) {}
    7. void addEntry(QString label, QPoint pos) {
    8. Entry e;
    9. e.label = label;
    10. e.pos = pos;
    11. QFontMetrics fm = fontMetrics();
    12. QRect r = fm.boundingRect(label);
    13. r.translate(e.pos);
    14. e.boundingBox = r;
    15. e.selected = false;
    16. m_entries << e;
    17. update();
    18. }
    19.  
    20. void removeEntry(int which) {
    21. m_entries.removeAt(which);
    22. update();
    23. }
    24.  
    25. int indexAt(QPoint pos) const {
    26. for(int i=0; i< m_entries.count(); ++i) {
    27. const Entry &e = m_entries.at(i);
    28. if(e.boundingBox.contains(pos))
    29. return i;
    30. }
    31. return -1;
    32. }
    33. protected:
    34. void paintEvent(QPaintEvent *pe) {
    35. QPainter p(this);
    36. foreach(Entry e, m_entries) {
    37. p.save();
    38. if(e.selected) {
    39. p.setPen(Qt::red);
    40. }
    41. p.drawText(e.pos, e.label);
    42. p.restore();
    43. // p.save();
    44. // p.setPen(Qt::red);
    45. // p.drawRect(e.boundingBox);
    46. // p.restore();
    47. }
    48. }
    49. void mouseReleaseEvent(QMouseEvent *me) {
    50. int idx = indexAt(me->pos());
    51. if(idx < 0) return;
    52. m_entries[idx].selected = !m_entries[idx].selected;
    53. update();
    54. }
    55.  
    56. private:
    57. struct Entry {
    58. QString label;
    59. QPoint pos;
    60. QRect boundingBox;
    61. bool selected;
    62. };
    63.  
    64. QList<Entry> m_entries;
    65. };
    66.  
    67.  
    68. #include "main.moc"
    69.  
    70. int main(int argc, char **argv) {
    71. QApplication app(argc, argv);
    72. Widget w;
    73. w.show();
    74. w.addEntry("Home", QPoint(100, 100));
    75. w.addEntry("School", QPoint(300, 200));
    76. w.addEntry("Work", QPoint(500, 20));
    77. return app.exec();
    78. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    CyrilQt (14th June 2012)

Similar Threads

  1. Replies: 1
    Last Post: 11th December 2011, 08:07
  2. to call member function
    By vinayaka in forum General Programming
    Replies: 5
    Last Post: 1st July 2011, 13:48
  3. Replies: 7
    Last Post: 1st July 2011, 01:21
  4. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 17:00
  5. function call
    By Walsi in forum Qt Programming
    Replies: 3
    Last Post: 12th June 2007, 09:13

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.