Results 1 to 4 of 4

Thread: Need some help porting from Qt3 to Qt4

  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; 26th November 2008 at 23:12. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need some help porting from Qt3 to Qt4

    Quote Originally Posted by psadhukhan View Post
    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
    Qt3 has single-threaded GUI as well. It was working just because of those lock/unlock macros.

    Quote Originally Posted by psadhukhan View Post
    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.
    I wonder how your application behaves when the user drags another window over it.

    Anyway, to solve your problem you might consider event-driven solution. You can make all those AWT_drawSomething functions emit a signal through a queued connection to the main thread that will draw something on a pixmap and schedule an update.

  3. #3
    Join Date
    Nov 2008
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Need some help


    Hi


    I am new to the Qt environment.i am developing an application in Qt 4.4 under windows platform.i want my application to run when my system reboots or restarts.for this i have to load my application(.exe or .ini files) into windows registry.can i have some code snippets for this.

    Thanks & regards,
    Srikanthch
    srikanthc49@gmail.com

  4. #4
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need some help porting from Qt3 to Qt4

    Thanks jacek
    I was trying an approach whereby I will use QImage [cannot use QPixmap as I guess painting into QPixmap is not allowed from nonGUI thread] as my offscreen buffer and tried painting into that and in paintEvent trying to flush QImage into the Qt window.
    Do you think this approach will work?

    I am attaching the native application I tried to modify to use this approach
    Qt Code:
    1. #include <pthread.h>
    2. #include <Qt>
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. #include <QPainter>
    7. #include <QWidget>
    8. #include <QImage>
    9.  
    10. class QPen;
    11. class QBrush;
    12. class QImage;
    13.  
    14. #define qtApp ((QtApplication *)qApp)
    15. void threadfunc(void *arg);
    16. void AWT_drawline(int x1, int y1, int x2, int y2);
    17. void getQtColorModel();
    18. void Qtexec();
    19.  
    20.  
    21. QImage img(800,600,QImage::Format_ARGB32);
    22.  
    23. class QtWindow : public QWidget
    24. {
    25. QBitmap transMask;
    26.  
    27. public:
    28. QtWindow(int flags,
    29. const char *name = "Sun AWT/Qt",
    30. QWidget *parent = NULL) ;
    31. virtual void paintEvent(QPaintEvent *);
    32.  
    33. };
    34.  
    35. class QtApplication : public QApplication {
    36. public :
    37. QtApplication(int &argc, char **argv);
    38. int exec(); //overloadded exec from QApplication
    39. };
    40.  
    41. class QtScreen
    42. {
    43. protected :
    44. int m_x;
    45. int m_y;
    46. int m_width;
    47. int m_height;
    48. QtWindow *m_window;
    49. bool m_bounds_restricted;
    50. /* virtual char **getArgs(int *argc);
    51.   virtual void computeBounds();
    52.   virtual void createQtWindow();
    53.   virtual void windowShown();
    54. */
    55.  
    56. public :
    57. QtScreen();
    58. bool init();
    59. /* void close();
    60.   void showWindow();
    61.   void setMouseCursor(int cursor);
    62.   void beep();
    63.   int width();
    64.   int height();
    65.   int dotsPerInch();
    66.   QtWindow *window();
    67.   int x();
    68.   int y();
    69. */
    70. };
    71.  
    72. class QtScreenFactory
    73. {
    74. static QtScreen *theScreen;
    75. public:
    76. static QtScreen *getScreen();
    77. };
    78.  
    79.  
    80. QtWindow *m_window;
    81. QtScreen *QtScreenFactory::theScreen = NULL;
    82. QtApplication::QtApplication(int &argc, char **argv) : QApplication(argc, argv) {
    83. }
    84.  
    85. int
    86. QtApplication::exec(void) {
    87. printf("QApplication exec called\n");
    88. QApplication::exec();
    89. printf("QApplication exec done\n");
    90. }
    91.  
    92. QtWindow::QtWindow(int flags, const char *name, QWidget *parent) :
    93. QWidget(parent, (Qt::WindowFlags)flags)
    94. {
    95. setMouseTracking(true);
    96. }
    97.  
    98. void
    99. QtWindow::paintEvent(QPaintEvent *event)
    100. {
    101. printf("paintEvent called\n");
    102. #if 0
    103. QPen *qp = new QPen();
    104. QColor c(0xffff0000);
    105. qp->setColor(c);
    106. QBrush *qb = new QBrush();
    107. QPainter p(this);
    108. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    109. p.setPen(*qp);
    110. p.setClipRect(0,0,800,600);
    111. p.drawLine(30,135,790,135);
    112. #endif
    113. QPainter p(m_window);
    114. QPoint pt(0,0);
    115. p.drawImage(pt, img);
    116. }
    117.  
    118. int main(int argc, char **argv)
    119. {
    120. printf("argc %d, argv %s\n",argc, *argv);
    121. QtScreen *screen = QtScreenFactory::getScreen();
    122. // img = new QImage(800,600,QImage::Format_ARGB32);
    123. }
    124.  
    125. QtScreen *
    126. QtScreenFactory::getScreen() {
    127. if ( QtScreenFactory::theScreen == NULL ) {
    128. QtScreenFactory::theScreen = new QtScreen();
    129. if(QtScreenFactory::theScreen != NULL) {
    130. QtScreenFactory::theScreen->init();
    131. }
    132. }
    133. return QtScreenFactory::theScreen;
    134. }
    135.  
    136. QtScreen::QtScreen()
    137. {
    138. }
    139.  
    140. void getQtColorModel()
    141. {
    142. int depth = 0;
    143. int amask=0,rmask=0,gmask=0,bmask=0;
    144.  
    145. QWidget *d = QApplication::desktop();
    146. depth = d->depth();
    147. printf ("Depth = %d\n", depth);
    148. switch(depth) {
    149. case 32:
    150. amask = 0xff000000;
    151. case 24:
    152. rmask = 0x00ff0000;
    153. gmask = 0x0000ff00;
    154. bmask = 0x000000ff;
    155. break;
    156. }
    157. }
    158.  
    159. void Qtexec()
    160. {
    161. qtApp->exec();
    162. }
    163.  
    164. bool
    165. QtScreen::init()
    166. {
    167. pthread_t tid;
    168. int m_x = 0,m_y=0,m_width=800,m_height=600;
    169.  
    170. int argc = 1;
    171. char *argv[3] = {"a.out", "-qws", NULL};
    172. new QtApplication(argc, argv);
    173. m_window = new QtWindow(Qt::FramelessWindowHint|Qt::Window);
    174. QBitmap bitmap(m_width, m_height);
    175. qtApp->setActiveWindow(m_window);
    176.  
    177. QPaintDevice *qpd = m_window;
    178. printf("exec called\n");
    179. m_window->show();
    180. getQtColorModel();
    181. #if 1
    182. pthread_create(&tid, NULL, (void *(*)(void *))threadfunc, NULL);
    183. #endif
    184. Qtexec();
    185. }
    186.  
    187.  
    188. void threadfunc(void *arg)
    189. {
    190. printf("threadfunc called\n");
    191. AWT_drawline(30,135,790,135);
    192. }
    193.  
    194. void
    195. AWT_drawline(int x1, int y1, int x2, int y2)
    196. {
    197. #if 0
    198. QPen *qp = new QPen();
    199. QBrush *qb = new QBrush();
    200. QPainter p(qpd);
    201. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    202. p.setPen(*qp);
    203. p.setClipRect(0,0,800,600);
    204. p.drawLine(30,135,790,135);
    205. #endif
    206. QPainter p(&img);
    207. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    208. QPen *qp = new QPen();
    209. QColor c(0xffff0000);
    210. qp->setColor(c);
    211. QBrush *qb = new QBrush();
    212. p.setPen(*qp);
    213. p.setClipRect(0,0,800,600);
    214. p.drawLine(x1, y1, x2, y2);
    215. m_window->update(0,0,800,600);
    216. }
    To copy to clipboard, switch view to plain text mode 

    But while executing this, I got a segv
    (gdb) run
    Starting program: /export/WorkSpaces/a.out
    [Thread debugging using libthread_db enabled]
    [New Thread -1208498480 (LWP 3723)]
    argc 1, argv /export/WorkSpaces/a.out
    Qt: gdb: -nograb added to command-line options.
    Use the -dograb option to enforce grabbing.
    exec called
    Depth = 24
    [New Thread -1212621936 (LWP 3727)]
    QApplication exec called
    threadfunc called

    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1212621936 (LWP 3727)]
    0x0077021d in QWidget::testAttribute (this=0x0,
    attribute=Qt::WA_WState_Visible)
    at ../../include/QtGui/../../src/gui/kernel/qwidget.h:986
    986 return data->widget_attributes & (1<<attribute);
    (gdb) bt
    #0 0x0077021d in QWidget::testAttribute (this=0x0,
    attribute=Qt::WA_WState_Visible)
    at ../../include/QtGui/../../src/gui/kernel/qwidget.h:986
    #1 0x007702e3 in QWidget::isVisible (this=0x0)
    at ../../include/QtGui/../../src/gui/kernel/qwidget.h:948
    #2 0x009632ee in QWidget::update (this=0x0, r=@0xb7b8d338)
    at painting/qbackingstore.cpp:1203
    #3 0x0804b2d7 in QWidget::update (this=0x0, ax=0, ay=0, aw=800, ah=600)
    at /usr/local/Trolltech/QtDebug-4.4.3/include/QtGui/qwidget.h:945
    #4 0x0804a8bb in AWT_drawline (x1=30, y1=135, x2=790, y2=135)
    at drawline.cpp:214
    #5 0x0804a924 in threadfunc (arg=0x0) at drawline.cpp:190
    #6 0x0050044b in start_thread () from /lib/libpthread.so.0
    #7 0x0047780e in clone () from /lib/libc.so.6
    (gdb)

    Can you please tell me as to what might be wrong in the application?

    If you feel this is not the right approach, then can you please tell me how to use the event-driven approach. Which class to use and how in the above appln.?

    Thanks in advance

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, 12:39
  2. Common question about porting Qt3 to Qt4
    By zlatko in forum Qt Programming
    Replies: 4
    Last Post: 21st February 2007, 10: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, 19:40
  4. Porting QT3 or 4
    By antonyang in forum Newbie
    Replies: 1
    Last Post: 3rd October 2006, 16:57
  5. Problem in porting Main window on linux
    By jyoti kumar in forum Qt Tools
    Replies: 2
    Last Post: 2nd June 2006, 08: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.