Results 1 to 6 of 6

Thread: paintEvent problem

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default paintEvent problem

    I am using the Qt plugin for Eclipse and I am following one of the tutorials for Qt 4.3.

    I have this:

    Qt Code:
    1. void cannonfield::setAngle(int angle)
    2. {
    3. if (angle < 5)
    4. angle = 5;
    5. if (angle > 70)
    6. angle = 70;
    7. if (currentAngle == angle)
    8. return;
    9. currentAngle = angle;
    10. update();
    11. emit angleChanged(currentAngle);
    12. }
    13.  
    14. void cannonfield::paintEvent(QPaintEvent * /* event */)
    15. {
    16. QPainter painter(this);
    17. painter.drawText(200, 200, tr("Angle = ") + QString::number(currentAngle));
    18. }
    To copy to clipboard, switch view to plain text mode 

    setAngle is called by:
    Qt Code:
    1. void lcdrange::on_horizontalSlider_valueChanged(int value)
    2. {
    3. ui.lcdNumber->display(value);
    4. this->setValue(value);
    5. ui.cannon->setAngle(value);
    6. }
    To copy to clipboard, switch view to plain text mode 

    The program will run. However, when it is run, nothing is displayed I am trying to get the angle value to be displayed in the "cannonfield" widget. The cannonfield widget is there, but the angle is not displayed. I put:
    Qt Code:
    1. printf("test");
    To copy to clipboard, switch view to plain text mode 
    into the paintEvent method and nothing is displayed in the console when I move the slider around. However, after I close the window, losts of "test"s are displayed.

    Also, how does the keyword "emit" work?

    Thanks.

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent problem

    Did you try setting a brush?

    Qt Code:
    1. void cannonfield::paintEvent(QPaintEvent * /* event */)
    2. {
    3. QPainter painter(this);
    4. painter.setPen(Qt::black);
    5. painter.setFont(font());
    6. painter.drawText(200, 200, tr("Angle = ") + QString::number(currentAngle));
    7. }
    To copy to clipboard, switch view to plain text mode 

    You should use qDebug() instead of printf() or call fflush(stdout);

    emit should work if you connected the signal to some slot and didn't forget to define the Q_OBJECT macro.

  3. #3
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: paintEvent problem

    Did not work after I set the pen. I think I don't have the signal connected to a slot correctly. I have:

    Qt Code:
    1. connect(ui.cannon, SIGNAL(angleChanged(int)), this, SLOT(setValue(int)));
    To copy to clipboard, switch view to plain text mode 

    In the constructor of my lcdrange class. Also, where is the documentation for qDebug?

  4. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent problem

    Can you post the code of your main widget as well as the full code of the cannonfield?

    In the main documentation page, at the left side, under "API Reference", click "All functions". Use the search function from your browser to find it in the list. Here is the direct link.

  5. #5
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: paintEvent problem

    Thanks. Code is attached. If you want to see the .h files or the ui files I can upload those as well.
    Attached Files Attached Files

  6. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paintEvent problem

    cannonfield is working for me. Aren't you sure the problem is somewhere else?
    Attached Files Attached Files

Similar Threads

  1. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  2. paintEvent problem
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 29th June 2008, 14:18
  3. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.