> Hi, thanks for the detailed example.
> 
> I guess the problem is, the async QWebPage is not exactly the same as a
> background thread. I use this url for
> testing http://doc.trolltech.com/qq/qq20-jambi.html
> 
> The loading takes around 3 seconds, I feel slightly no responsive but still
> fine (the page.mainFrame().contentsSize() show the size is QSize(620,
> 54502)) . However, for some unknown reason,
> page.mainFrame().render(painter) just hang there causing no responsive UI.
> 
> So not running in a separate thread is ok for normal cases, but there are
> cases which may cause no responsive UI.....
> I may be wrong though, I do not know exactly what causes the problem for
> the above URL. Viewing in a QWebView is ok without any problem though.
> thanks,
> canal
You can use QCoreApplication.processEvents();  in the middle of some long time 
process  to avoid unresposive UI.
> 
> 
> 
> 
> ________________________________
> From: René Jahn <[email protected]>
> To: [email protected]
> Sent: Sat, August 21, 2010 4:31:46 PM
> Subject: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread
> 
> 
> Hi,
> 
> my preferred solution:
> 
>     void thumb() {
>       Thumbnailer thumb = new Thumbnailer();
>             thumb.load(lineEdit.text());
>     }
> 
>     class Thumbnailer extends QWebPage
>     {
>       Thumbnailer()
>       {
>             loadStarted.connect(this, "loadStarted()");
>             loadFinished.connect(this, "loadFinished(Boolean)");
>       }
> 
>       void load(String pText)
>       {
>             mainFrame().load(new QUrl(pText));
>       }
> 
>         void loadStarted()
>         {
>             System.out.println("START");
>         }
> 
>         void loadFinished(Boolean pState)
>         {
>          setViewportSize(mainFrame().contentsSize());
> 
>            QImage image = new QImage(viewportSize(),
> QImage.Format.Format_ARGB32);
>            QPainter painter = new QPainter(image);
> 
>            mainFrame().render(painter);
>            painter.end();
> 
>            // we assume that the page width is smaller than the page height
>            QImage thumbnail = image.scaledToWidth(218,
> Qt.TransformationMode.SmoothTransformation);
>            thumbnail.save("d:\\thumbnail.png");
> 
>            System.out.println ("rendering finished");
>         }
> 
>     }
> 
> The QWebPage works async. You start loading and the UI works without locks…
> After the page was loaded your method will be called.
> The UI works without locks…
> 
> You don’t need a Thread.
> 
> 
> To turn off thread-checks, general, call:
> 
> System.setProperty("com.trolltech.qt.thread-check", "no");
> 
> But be careful when you call UI updates from threads!!!
> Always use invokeLater!
> 
> 
> 
> René
> 
> Von:go canal [mailto:[email protected]]
> Gesendet: Samstag, 21. August 2010 02:55
> An: René Jahn; [email protected]
> Betreff: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread
> 
> Thank you very much.
> 
> I am using Windows XP, Jambi 4.5.2_01, running in Eclipse 3.6.
> The program has a back/forward button, a QWebView which shows the google
> web page by default. And a 'thumb' button, a url text line. Type a url,
> then click 'thumb' button, it suppose to capture the web page and save the
> image to a file 'thumbnail.png'.
> When running the program, I can see the google web page. In
> ThumbnailThread, load started signal is also captured, but no load
> finished signal. Upon clicking the 'thumb' again, I saw this error
> message:
> QObject::startTimer: timers cannot be started from another thread
> 
> 
> Source code
> ========
> 
> Main.java
> -----------
> public class Main extends QMainWindow {
> 
>     Ui_Main ui = new Ui_Main();
> 
>     public static void main(String[] args) {
>           //QNetworkProxy proxy = new QNetworkProxy(ProxyType.HttpProxy,
> hostname, 8080);
>           //QNetworkProxy.setApplicationProxy(proxy);
> 
>         QApplication.initialize(args);
> 
>         Main testMain = new Main();
>         testMain.show();
> 
>         QApplication.exec();
>     }
> 
>     public Main() {
>         ui.setupUi(this);
>     }
> 
>     public Main(QWidget parent) {
>         super(parent);
>         ui.setupUi(this);
>     }
> }
> 
> Ui_Main.java
> --------------
> public class Ui_Main implements com.trolltech.qt.QUiForm<QMainWindow>
> {
>     public QWidget centralwidget;
>     public QLineEdit lineEdit;
>     public QWebView webView;
>     public QPushButton back;
>     public QPushButton forward;
>     public QPushButton thumbButton;
>     public QMenuBar menubar;
>     public QStatusBar statusbar;
> 
>     public Ui_Main() { super(); }
> 
>     public void setupUi(QMainWindow Main)
>     {
>         Main.setObjectName("Main");
>         Main.resize(new QSize(800,
> 367).expandedTo(Main.minimumSizeHint())); centralwidget = new
> QWidget(Main);
>         centralwidget.setObjectName("centralwidget");
>         lineEdit = new QLineEdit(centralwidget);
>         lineEdit.setObjectName("lineEdit");
>         lineEdit.setGeometry(new QRect(100, 10, 631, 20));
>         webView = new QWebView(centralwidget);
>         webView.setObjectName("webView");
>         webView.setGeometry(new QRect(10, 40, 800, 600));
>         webView.setUrl(new QUrl("http://www.google.com/";));
>         back = new QPushButton(centralwidget);
>         back.setObjectName("back");
>         back.setGeometry(new QRect(10, 10, 21, 23));
>         forward = new QPushButton(centralwidget);
>         forward.setObjectName("forward");
>         forward.setGeometry(new QRect(40, 10, 21, 23));
>         thumbButton = new QPushButton(centralwidget);
>         thumbButton.setObjectName("thumbButton");
>         thumbButton.setGeometry(new QRect(750, 10, 41, 23));
>         Main.setCentralWidget(centralwidget);
>         menubar = new QMenuBar(Main);
>         menubar.setObjectName("menubar");
>         menubar.setGeometry(new QRect(0, 0, 800, 21));
>         Main.setMenuBar(menubar);
>         statusbar = new QStatusBar(Main);
>         statusbar.setObjectName("statusbar");
>         Main.setStatusBar(statusbar);
>         retranslateUi(Main);
>         back.clicked.connect(webView, "back()");
>         forward.clicked.connect(webView, "forward()");
>         thumbButton.clicked.connect(this, "thumb()");
> 
>         Main.connectSlotsByName();
>     } // setupUi
> 
>     void retranslateUi(QMainWindow Main)
>     {
> 
>  Main.setWindowTitle(com.trolltech.qt.core.QCoreApplication.translate("Main
> ", "MainWindow", null));
>        
> back.setText(com.trolltech.qt.core.QCoreApplication.translate("Main",
> "<-", null));
>        
> forward.setText(com.trolltech.qt.core.QCoreApplication.translate("Main",
> "->", null));
> 
>  thumbButton.setText(com.trolltech.qt.core.QCoreApplication.translate("Main
> ", "Thumb", null));
>     } // retranslateUi
> 
> 
>     void thumb() {
>           ThumbnailerThread t = new ThumbnailerThread (lineEdit.text());
>           QThread thread = new QThread (t);
>           //t.moveToThread(thread);
>           thread.start();
> 
> 
>     }
> }
> 
> ThumbnailerThread.java
> ------------------------
> public class ThumbnailerThread extends QObject implements Runnable {
>                       public Signal0 finished = new Signal0();
>                       private int width = 218;
>                       private int height = 128;
>                       private QWebPage page;
>                       private String url;
> 
>                       public ThumbnailerThread (String url) {
>                                   this.url = url;
>                       }
> 
>                       @Override
>                       public void run()
> {
> 
>                                   page = new QWebPage ();
>                                   page.loadStarted.connect(this,
> "loadStarted()");
> 
>           page.loadFinished.connect(this, "render(Boolean)");
>                                   page.mainFrame().load(new QUrl(url));
>                       }
> 
>                       public void loadStarted () {
>                                   System.out.println ("load started");
>                       }
> 
>                       public void render(Boolean b) {
>                                   System.out.println ("rendering now : " +
> page.mainFrame().contentsSize().toString());
>                                   // if the page is long, it may take long
> time. // for example,
> http://doc.trolltech.com/qq/qq20-jambi.html has a size of 620x54502
> 
> 
> 
> page.setViewportSize(page.mainFrame().contentsSize());
> 
>                  QImage image = new QImage(page.viewportSize(),
> QImage.Format.Format_ARGB32);
>                  QPainter painter = new QPainter(image);
> 
>                  page.mainFrame().render(painter);
>                  painter.end();
> 
>                  // we assume that the page width is smaller than the page
> height
>                  QImage thumbnail = image.scaledToWidth(width,
> Qt.TransformationMode.SmoothTransformation);
>                  thumbnail.save("thumbnail.png");
> 
>                  finished.emit();
> 
>                  System.out.println ("rendering finished");
>              }
> }
> 
> 
> 
> thanks,
> canal
> 
> 
> 
> ________________________________
> 
> From:René Jahn <[email protected]>
> To: [email protected]
> Sent: Sat, August 21, 2010 1:33:06 AM
> Subject: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread I tried a demo app and, it works as expected.
> 
> Please post a running app.
> 
> Which QTJambi version, which OS?
> 
> 
> Von:go canal [mailto:[email protected]]
> Gesendet: Freitag, 20. August 2010 16:13
> An: René Jahn; [email protected]
> Betreff: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread
> 
> now the problem is different:
> 
>   I have a button, when clicked, I start the thumbnail+scale thread.
> nothing happen but I do capture the page load start signal; can not
> capture the finish loading signal;
>   then I click the button again, got this error:
> 
> QObject::startTimer: timers cannot be started from another thread
> 
> I do not see this when first click the button.
> 
> 
> thanks,
> canal
> 
> 
> 
> ________________________________
> 
> From:René Jahn <[email protected]>
> To: [email protected]
> Sent: Fri, August 20, 2010 7:41:16 PM
> Subject: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread Are you behind a proxy?
> 
> 
> Von:[email protected]
> [mailto:[email protected]] Im Auftrag von go canal
> Gesendet: Freitag, 20. August 2010 12:52
> An: Helge Fredriksen
> Cc: [email protected]
> Betreff: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread
> 
> Hi,
> you are right, I do not need a thread to load a page; what I wanted to do
> is to capture the web page, scale it, and save as an image file. this may
> takes some time and I wanted to put it into a separate thread.
> 
> I modified a little bit (put new QWebPage() into run() method), the
> exception is gone; however, I am not able to capture the loadFinished
> signal (but can capture start loading signal):
> 
> public class Thumbnailer extends QObject implements Runnable {
>   QWebPage page;
>   String url;
> 
>   public Thumbnailer (String url) {
>      this.url = url;
>   }
> 
>   public void run() {
>      page = new QWebPage();  /* <<<< exception if I put this is in the
> constructor */
>      page.loadFinished.connect (this, "render(Boolean)");
>      page.mainFrame().load(new QUrl(url));
>   }
> 
>   private void renbder (Boolean b) {
>     .....
>   }
> }
> 
> thanks,
> canal
> 
> 
> 
> ________________________________
> 
> From:Helge Fredriksen <[email protected]>
> To: go canal <[email protected]>
> Cc: [email protected]
> Sent: Fri, August 20, 2010 3:17:05 PM
> Subject: Re: [Qt-jambi-interest] Error: QObject used from outside its own
> thread
> 
> Hello!
> 
> Have you studied the "Hello Webkit" example in our demo application? It
> seems that
> no extra thread is necessary to load a page, everything happens in the
> QWebView.load()
> method, and it seems to take place without freezing the UI.
> 
> Best regards,
> Helge Fredriksen
> 
> On 08/20/2010 03:40 AM, go canal wrote:
> > Hi Rene,
> > Thanks for your sharing. I can access the web, but I have problems
> > putting it into a different thread.
> > 
> > What I wanted to do is, upon clicking a button, the slot will create a
> > new thread:
> > ----
> > 
> >  Thumbnail nail = new Thumbnail (url);
> > 
> > QThread thread = new QThread (nailer);
> > thread.start();
> > ----
> > I though this is how I should create a new Thread in Qt.
> > 
> > The Thumbnail class is defined as:
> > 
> > public class Thumbnailer extends QObject implements Runnable {
> > 
> >        @Override
> > 
> > public void run() {
> > page.loadFinished.connect(this, "render(Boolean)");
> > 
> > page.mainFrame().load(new QUrl(url));
> > }
> > 
> >        ......
> > 
> > }
> > 
> > But apparently it does not work, exception at
> > page.mainFrame().load(new QUrl(url));
> > 
> > I can not figure out what's wrong....
> > 
> > thanks,
> > canal
> > 
> > 
> > ------------------------------------------------------------------------
> > *From:* René Jahn <[email protected]>
> > *To:* [email protected]
> > *Sent:* Thu, August 19, 2010 3:49:11 PM
> > *Subject:* Re: [Qt-jambi-interest] Error: QObject used from outside
> > its own thread
> > 
> > It should work with following code:
> >                /**
> >                
> >                  * The <code>Main</code> class handles the web access.
> >                  
> >                  *
> >                  
> >                  * @author René Jahn
> >                  
> >                  */
> >                
> >                private static final class Main extends QObject
> >                
> >                {
> > 
> > //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >                                // Class members
> > 
> > //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >                                /** data loaded event. */
> >                                
> >                                private final
> > 
> > QSignalEmitter.Signal1<String> loaded = new
> > QSignalEmitter.Signal1<String>();
> > 
> >                                /** reload event. */
> >                                
> >                                private final QSignalEmitter.Signal0
> > 
> > reload = new QSignalEmitter.Signal0();
> > 
> >                                /** web view. */
> >                                
> >                                QWebView web;
> > 
> > //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >                                // Initialization
> > 
> > //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >                                /**
> >                                
> >                                * Creates a new instance of
> > 
> > <code>Main</code> and starts reading the
> > 
> >                                * web page.
> >                                
> >                                */
> >                                
> >                                Main()
> >                                
> >                                {
> >                                
> >                                                if
> > 
> > (System.getProperty("http.proxyHost") != null)
> > 
> >                                                {
> >                                                
> >                                                                try
> >                                                                
> >                                                                {
> > 
> > QNetworkProxy proxy = new QNetworkProxy(ProxyType.HttpProxy);
> > 
> > 
> > proxy.setHostName(System.getProperty("http.proxyHost"));
> > 
> > 
> > proxy.setPort(Integer.parseInt(System.getProperty("http.proxyPort")));
> > 
> > 
> > QNetworkProxy.setApplicationProxy(proxy);
> > 
> >                                                                }
> >                                                                
> >                                                                catch
> > 
> > (Exception e)
> > 
> >                                                                {
> > 
> > e.printStackTrace();
> > 
> >                                                                }
> >                                                
> >                                                }
> >                                                
> >                                                web = new QWebView();
> > 
> > web.loadFinished.connect(this, "loadFinished(Boolean)");
> > 
> >                                                reload.connect(this,
> > 
> > "reloadIntern()");
> > 
> >                                }
> > 
> > //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >                                // User-defined methods
> > 
> > //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 
> >                                /**
> >                                
> >                                * Triggers the web page reload.
> >                                
> >                                */
> >                                
> >                                public void reload()
> >                                
> >                                {
> >                                
> >                                                //needed to avoid
> > 
> > threading problems!
> > 
> >                                                reload.emit();
> >                                
> >                                }
> >                                
> >                                /**
> >                                
> >                                * Reloades the web page.
> >                                
> >                                */
> >                                
> >                                @SuppressWarnings("unused")
> >                                
> >                                private void reloadIntern()
> >                                
> >                                {
> >                                
> >                                                web.load(new
> > 
> > QUrl("http://qtjambi.sourceforge.net";));
> > 
> >                                }
> >                                
> >                                /**
> >                                
> >                                * Forwards the html content to the
> > 
> > signal handler.
> > 
> >                                *
> >                                
> >                                * @param pLoaded the loaded state
> >                                
> >                                */
> >                                
> >                                @SuppressWarnings("unused")
> >                                
> >                                private void loadFinished(Boolean pLoaded)
> >                                
> >                                {
> > 
> > loaded.emit(web.page().mainFrame().toHtml());
> > 
> >                                }
> >                
> >                }              // Main
> > 
> > Regards,
> > 
> > René
> > 
> > *Von:* [email protected]
> > [mailto:[email protected]] *Im Auftrag von *go
> > canal *Gesendet:* Donnerstag, 19. August 2010 03:38
> > *An:* [email protected]
> > *Betreff:* [Qt-jambi-interest] Error: QObject used from outside its
> > own thread
> > 
> > Hello,
> > 
> > New to Qt Jambi, sorry if this is already been asked before.
> > 
> > I got an exception:
> > 
> > -----------------------------------------------------------
> > 
> > Exception in thread "Thread-2" QObject used from outside its own
> > thread, object=QWebPage(0x17b786e8) ,
> > objectThread=Thread[main,5,main], currentThread=Thread[Thread-2,5,main]
> > 
> > at
> > com.trolltech.qt.GeneratorUtilities.threadCheck(GeneratorUtilities.java:5
> > 9)
> > 
> > at com.trolltech.qt.webkit.QWebPage.mainFrame(QWebPage.java:619)
> > 
> > at Thumbnailer.run(Thumbnailer.java:44)
> > 
> > at java.lang.Thread.run(Thread.java:619)
> > 
> > at com.trolltech.qt.QThread.run(QThread.java:164)
> > 
> > -----------------------------------------------------------
> > 
> > Could not figure out what's wrong.  Here is my code
> > 
> > In the UI_Main :
> > 
> > {
> > 
> > ......
> > 
> > button.clicked.connect(this, "thumb()");
> > 
> > ......
> > 
> > }
> > 
> > void thumb() {
> > 
> > Thumbnailer nailer = new Thumbnailer (lineEdit.text());
> > 
> > QThread thread = new QThread (nailer);
> > 
> > nailer.moveToThread(thread);
> > 
> > thread.start();
> > 
> > }
> > 
> > Here is the Thumbnail class (download a web page and create a
> > thumbnail image):
> > 
> > public class Thumbnailer extends QObject implements Runnable {
> > 
> > ......
> > 
> > @Override
> > 
> > public void run() {
> > 
> > page.loadFinished.connect(this, "render(Boolean)");
> > 
> > page.mainFrame().load(new QUrl(url));
> > 
> > }
> > 
> > ......
> > 
> > }
> > 
> > What did I do wrongly ?
> > 
> > many thanks,
> > canal
> > 
> > 
> > 
> > _______________________________________________
> > Qt-jambi-interest mailing list
> > [email protected]
> > http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest
-- 
Ing. José Arcángel Salazar Delgado
Nettrace
Av 27 #6 col. Venustiano Carranza.
Boca del río, Ver.
Tel. Oficina 9-27-54-78

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to