Results 1 to 4 of 4

Thread: how to create a new function

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default how to create a new function

    Hello,

    I have a very simple program below that draws a very simple picture (just one dot) and assigns that picture to a widget. Widget is then added to main layout and displayed. Everything works great. Now I'd like the drawing part to take place in another function. I'd like a function draw() to call from main function, draw this little dot and return to main(). All my attempts to achieve that have failed because I'm not sure how the new function should be declared/created (what arguments to use). Am I supposed to create a new class for that function? Or use canvas? I know this must be a very simple thing to do but I just can't think of it. I'm very thankful for any guidance.

    #include <QtGui>
    using namespace std;

    int main(int argc, char *argv[])
    {
    QApplication application(argc, argv);
    QWidget window;
    QVBoxLayout* mainLayout = new QVBoxLayout(&window);
    QLabel* pictureLabel = new QLabel;

    ////start: i want this to be in another function
    QPixmap pm(100,100);
    QPainter p(&pm);
    QPen pen(Qt::white, 10);
    p.setPen(pen);
    p.drawPoint(20,20);
    ////end: i want this to be in another function

    pictureLabel->setPixmap(pm);
    mainLayout->addWidget(pictureLabel);

    window.show();
    return application.exec();
    }

  2. #2
    Join Date
    Jun 2007
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to create a new function

    I'm not sure I understand what you really intend to do and where the problem is. Is this what you are looking for?

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. QPixmap drawStuff();
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication application(argc, argv);
    8. QWidget window;
    9. QVBoxLayout* mainLayout = new QVBoxLayout(&window);
    10. QLabel* pictureLabel = new QLabel;
    11.  
    12. pictureLabel->setPixmap(drawStuff());
    13. mainLayout->addWidget(pictureLabel);
    14.  
    15. window.show();
    16. return application.exec();
    17. }
    18.  
    19. QPixmap drawStuff()
    20. {
    21. QPixmap pm(100,100);
    22. QPainter p(&pm);
    23. QPen pen(Qt::white, 10);
    24. p.setPen(pen);
    25. p.drawPoint(20,20);
    26. return pm;
    27. }
    To copy to clipboard, switch view to plain text mode 

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

    tommy (10th November 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default thanks danbetz

    Thanks a lot. You answered my question 100% !!

  5. #4
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default

    --------------------------
    Last edited by tommy; 10th November 2007 at 21:19.

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08: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.