Results 1 to 4 of 4

Thread: Need some help porting from Qt3 to Qt4

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Need some help porting from Qt3 to Qt4

    Hi,

    I am a newbie to Qt world. We have a product which follows Java AWT model (which is multithread enabled in that from any Java thread, the AWT libs will try to make GUI calls) and was working in Qt3 but it seems that the new Qt4 model is single threaded and wants all GUI calls to be only made from the main thread [the thread that calls exec()] as the tutorial seems to imply
    Note that QCoreApplication::exec() must always be called from the main thread (the thread that executes main()), not from a QThread. In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations.

    Also, there is one more change in Qt4 w.r.t Qt3
    When implementing custom widgets in Qt 3, it was possible to use QPainter to draw on a widget outside paint events
    In Qt 4, it is only possible to paint on a widget from within its paintEvent() handler function. This restriction simplifies Qt's interaction with native window systems, improves the performance of applications by reducing the number of redraw operations, and also enables features to be implemented to improve the appearance of widgets, such as a backing store. Generally, we recommend redesigning applications to perform all painting operations in paintEvent() functions, deferring actual painting until the next time this function is called. Applications can post paint events to trigger repaints, and it may be possible to examine your widget's internal state to determine which part of the widget needs to be repainted


    so, in Qt3, we did not have any paintEvent() method and whenever AWT lib calls any GUI related function like drawLine, drawRect, drawFont, drawImage it used to draw the shapes in the Qt window. One such example:
    Qt Code:
    1. AWT_drawLine(int x1, int y1, int x2, int y2)
    2. {
    3. AWT_QT_LOCK; {
    4. QPainter p(QtImageDescPool[QtGraphDescPool[qtGraphDesc].qid].qpd);
    5. #ifdef QT_VER_4
    6. p.setCompositionMode(QtGraphDescPool[qtGraphDesc].rasterOp);
    7. #else
    8. p.setRasterOp(QtGraphDescPool[qtGraphDesc].rasterOp);
    9. #endif
    10. p.setPen(*((QPen *)(QtGraphDescPool[qtGraphDesc].qp)));
    11. setPainterClip(&p, qtGraphDesc);
    12. p.drawLine(x1, y1, x2, y2);
    13.  
    14. printf("DrawLine called\n");
    15. if(maskPainter(qtGraphDesc, p))
    16. p.drawLine(x1, y1, x2, y2);
    17. }
    18. AWT_QT_UNLOCK;
    19. }
    To copy to clipboard, switch view to plain text mode 

    In Qt3 [when QT_VER_4] was not defined, it used to work as paintEvent was not needed. But, I guess in Qt4, this way of execution will not display anything. I have created a native application which does the drawing in paintEvent() and the application work provided paintEvent is present. But, it's difficult to satisfy our AWT model if we have to draw in paintEvent()

    I was thinking of the thread that executes exec() to draw all the GUI operations on offscreen surface[back buffer] and in paintEvent, periodically display the back buffer into the Qt screen.
    Will that be a good way of doing it in Qt4? If not, can you please suggest some way?
    If yes, can you please tell me how to create an offscreen [back buffer] [will that be a Qwidget] and how can we flush the back buffer into Qt screen?
    Can you please tell me how can I modify the attached application so that drawline() function will draw the line in back buffer and paintEvent will take the back-buffer and flush on the Qt screen?

    Thanks in advance.
    Regards
    Prasanta

    The native application:
    Qt Code:
    1. #include <Qt>
    2. #include <QtCore>
    3. #include <QtGui>
    4. #include <QPainter>
    5. #include <QWidget>
    6.  
    7. class QPen;
    8. class QBrush;
    9.  
    10. #define qtApp ((QtApplication *)qApp)
    11. void drawline(QPaintDevice *qpd);
    12.  
    13. class QtWindow : public QWidget
    14. {
    15. QBitmap transMask;
    16.  
    17. public:
    18. QtWindow(int flags,
    19. const char *name = "Sun AWT/Qt",
    20. QWidget *parent = NULL) ;
    21. virtual void paintEvent(QPaintEvent *);
    22.  
    23. };
    24.  
    25. class QtApplication : public QApplication {
    26. public :
    27. QtApplication(int &argc, char **argv);
    28. int exec(); //overloadded exec from QApplication
    29. };
    30.  
    31. QtApplication::QtApplication(int &argc, char **argv) : QApplication(argc, argv) {
    32. }
    33.  
    34. int
    35. QtApplication::exec(void) {
    36. printf("QApplication exec called\n");
    37. QApplication::exec();
    38. printf("QApplication exec done\n");
    39. }
    40.  
    41. QtWindow::QtWindow(int flags, const char *name, QWidget *parent) :
    42. QWidget(parent, (Qt::WindowFlags)flags)
    43. {
    44. setMouseTracking(true);
    45. }
    46.  
    47. void
    48. QtWindow::paintEvent(QPaintEvent *event)
    49. {
    50. #if 0
    51. printf("paintEvent called\n");
    52. QPen *qp = new QPen();
    53. QBrush *qb = new QBrush();
    54. QPainter p(this);
    55. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    56. p.setPen(*qp);
    57. p.setClipRect(0,0,800,600);
    58. p.drawLine(30,135,790,135);
    59. #endif
    60. }
    61.  
    62. int main(int argc, char **argv)
    63. {
    64. printf("argc %d, argv %s\n",argc, *argv);
    65. int m_x = 0,m_y=0,m_width=800,m_height=600;
    66.  
    67. new QtApplication(argc, argv);
    68. QtWindow *m_window = new QtWindow(Qt::FramelessWindowHint|Qt::Window);
    69. QBitmap bitmap(m_width, m_height);
    70. qtApp->setActiveWindow(m_window);
    71.  
    72. QPaintDevice *qpd = m_window;
    73. #if 1
    74. drawline(qpd);
    75. #endif
    76. printf("exec called\n");
    77. m_window->show();
    78. qtApp->exec();
    79. }
    80.  
    81. void
    82. drawline(QPaintDevice *qpd)
    83. {
    84. QPen *qp = new QPen();
    85. QBrush *qb = new QBrush();
    86. QPainter p(qpd);
    87. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    88. p.setPen(*qp);
    89. p.setClipRect(0,0,800,600);
    90. p.drawLine(30,135,790,135);
    91. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 27th November 2008 at 00:12. Reason: missing [code] tags

Similar Threads

  1. Porting from qwt-4.2.0 to qwt-5.0.2
    By vheinitz in forum Qwt
    Replies: 3
    Last Post: 31st January 2008, 13:39
  2. Common question about porting Qt3 to Qt4
    By zlatko in forum Qt Programming
    Replies: 4
    Last Post: 21st February 2007, 11:57
  3. Problem porting Kivio MP on win32 from Qt 3 to Qt 4
    By Amanda in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2006, 20:40
  4. Porting QT3 or 4
    By antonyang in forum Newbie
    Replies: 1
    Last Post: 3rd October 2006, 17:57
  5. Problem in porting Main window on linux
    By jyoti kumar in forum Qt Tools
    Replies: 2
    Last Post: 2nd June 2006, 09:35

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.