Results 1 to 7 of 7

Thread: An example puzzle me, please help

  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Question An example puzzle me, please help

    Qt Code:
    1. //ticker.h
    2. #ifndef TICKER_H
    3. #define TICKER_H
    4.  
    5. #include <QWidget>
    6.  
    7. class Ticker : public QWidget
    8. {
    9. Q_OBJECT
    10. Q_PROPERTY(QString text READ text WRITE setText)
    11.  
    12. public:
    13. Ticker(QWidget *parent = 0);
    14.  
    15. void setText(const QString &newText);
    16. QString text() const { return myText; }
    17. QSize sizeHint() const;
    18.  
    19. protected:
    20. void paintEvent(QPaintEvent *event);
    21. void timerEvent(QTimerEvent *event);
    22. void showEvent(QShowEvent *event);
    23. void hideEvent(QHideEvent *event);
    24.  
    25. private:
    26. QString myText;
    27. int offset;
    28. int myTimerId;
    29. };
    30.  
    31. #endif
    32.  
    33.  
    34. //ticker.cpp
    35. #include <QtGui>
    36.  
    37. #include "ticker.h"
    38.  
    39. Ticker::Ticker(QWidget *parent)
    40. : QWidget(parent)
    41. {
    42. offset = 0;
    43. myTimerId = 0;
    44. }
    45.  
    46. void Ticker::setText(const QString &newText)
    47. {
    48. myText = newText;
    49. update();
    50. updateGeometry();
    51. }
    52.  
    53. QSize Ticker::sizeHint() const
    54. {
    55. return fontMetrics().size(0, text());
    56. }
    57.  
    58. void Ticker::paintEvent(QPaintEvent * /* event */)
    59. {
    60. QPainter painter(this);
    61.  
    62. int textWidth = fontMetrics().width(text());
    63. if (textWidth < 1)
    64. return;
    65. int x = -offset;
    66. while (x < width()) {
    67. painter.drawText(x, 0, textWidth, height(),
    68. Qt::AlignLeft | Qt::AlignVCenter, text());
    69. x += textWidth;
    70. }
    71. }
    72.  
    73. void Ticker::showEvent(QShowEvent * /* event */)
    74. {
    75. myTimerId = startTimer(30);
    76. }
    77.  
    78. void Ticker::timerEvent(QTimerEvent *event)
    79. {
    80. if (event->timerId() == myTimerId) {
    81. ++offset;
    82. if (offset >= fontMetrics().width(text()))
    83. offset = 0;
    84. scroll(-1, 0);
    85. } else {
    86. QWidget::timerEvent(event);
    87. }
    88. }
    89.  
    90. void Ticker::hideEvent(QHideEvent * /* event */)
    91. {
    92. killTimer(myTimerId);
    93. myTimerId = 0;
    94. }
    95.  
    96. //main.cpp
    97. #include <QApplication>
    98.  
    99. #include "ticker.h"
    100.  
    101. int main(int argc, char *argv[])
    102. {
    103. QApplication app(argc, argv);
    104. Ticker ticker;
    105. ticker.setWindowTitle(QObject::tr("Ticker"));
    106. ticker.setText(QObject::tr("How long it lasted was impossible to "
    107. "say ++ "));
    108. ticker.show();
    109. return app.exec();
    110. }
    To copy to clipboard, switch view to plain text mode 

    This is an example from C++ GUI programming with Qt4 , second edition.

    This example puzzle me.

    1 , Q_PROPERTY(QString text READ text WRITE setText). Q_PROPERTY macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System. But I do think I can do the same thing with QString and the function I write the same with the Q_PROPERTY declare. What's the function of this Q_PROPERTY?

    2 , Check all over the code, I can not find any place has called showEvent(), for me, it should not start the timer. But in fact, it does start it and work well. Why?

    3 int x = -offset;
    while (x < width()) {
    painter.drawText(x, 0, textWidth, height(),
    Qt::AlignLeft | Qt::AlignVCenter, text());
    //.......

    The first time of drawText will draw a part of the text outside the widget, and only the fragment inside the widget will show, right?

    4 scroll(-1,0); //Scrolls the widget including its children dx pixels to the right and dy downward. Both dx and dy may be negative. After scrolling, the widgets will receive paint events for the areas that need to be repainted. That is to say, the widget can be very long, outside the viewport and only will draw text in the viewport?


    Still know little, Thanks for your help!

  2. #2
    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: An example puzzle me, please help

    Quote Originally Posted by HelloDan View Post
    1 , Q_PROPERTY(QString text READ text WRITE setText). Q_PROPERTY macro is used for declaring properties in classes that inherit QObject. Properties behave like class data members, but they have additional features accessible through the Meta-Object System. But I do think I can do the same thing with QString and the function I write the same with the Q_PROPERTY declare. What's the function of this Q_PROPERTY?
    As you quoted it gives you additional features. Like the ability to set the property value from Designer or from a script. In addition to that you don't have to know that a certain property/method exists to be able to use it - you can query the meta-object of the class for a list of methods (signals, slots, properties) and use a pair of generic QObject::property() and QObject::setProperty() methods to change their values. This is especially useful when providing objects through a plugin. There are also additional benefits when using QWizard or QDataWidgetMapper.

    2 , Check all over the code, I can not find any place has called showEvent(), for me, it should not start the timer. But in fact, it does start it and work well. Why?
    showEvent() is an event handler called automatically by Qt when the widget is shown (i.e. by calling QWidget::show()).

    The first time of drawText will draw a part of the text outside the widget, and only the fragment inside the widget will show, right?
    Yes, that's correct.

    4 scroll(-1,0); //Scrolls the widget including its children dx pixels to the right and dy downward. Both dx and dy may be negative. After scrolling, the widgets will receive paint events for the areas that need to be repainted. That is to say, the widget can be very long, outside the viewport and only will draw text in the viewport?
    The widget is always clipped to its parent. So if the widget is larger than the parent, only the part contained in the parent's rectangle will be visible.
    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.


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

    HelloDan (7th March 2009)

  4. #3
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: An example puzzle me, please help

    Thanks wysota

    But I try without Q_PROPERTY(QString text READ text WRITE setText).

    It can do the same things. I have not yet finished the C++ GUI programming with Qt4, but just chapter 12. Can you give me an example or a link to see the inside feature of Q_PROPERTY?

    Thanks!

  5. #4
    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: An example puzzle me, please help

    Quote Originally Posted by HelloDan View Post
    Thanks wysota

    But I try without Q_PROPERTY(QString text READ text WRITE setText).

    It can do the same things.
    You can use setProperty() on the property and it changes the text used by the widget? If so then it means you didn't rebuild the application properly.
    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.


  6. #5
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: An example puzzle me, please help

    Quote Originally Posted by wysota View Post
    You can use setProperty() on the property and it changes the text used by the widget? If so then it means you didn't rebuild the application properly.
    I have no idea.

    I delete the Q_PROPERTY line. and rebuild in this way.

    make clean
    make


    It can do the same thing.

    Maybe now I have not the foundation to comprehend the Q_PROPERTY.


    bool QObject::setProperty ( const char * name, const QVariant & value )
    Sets the value of the object's name property to value.
    If the property is defined in the class using Q_PROPERTY then true is returned on success and false otherwise. If the property is not defined using Q_PROPERTY, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.
    Information about all available properties is provided through the metaObject() and dynamicPropertyNames().

    What for ? It puzzles me further more. Can you give me an example? Or I can refer to what material? Thanks!

  7. #6
    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: An example puzzle me, please help

    Quote Originally Posted by HelloDan View Post
    It can do the same thing.
    Because you are not using anything Q_PROPERTY offers. If I have a car and I don't use the wipes, I can take them off and say the car still behaves as it did before. Q_PROPERTY is an extra functionality that you are simply not using at the time.

    Please read about Q_PROPERTY and meta-object system in Qt Assistant. It's a wide topic, impossible to cover in a forum thread.
    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.


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

    HelloDan (8th March 2009)

  9. #7
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: An example puzzle me, please help

    Thank you! Maybe I should put it aside now. But I will learn further more and master it.

    Thanks again!

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.