Instantiate QWidget in QCoreApplication



  • Is there any way I can instantiate a QWidget in a QCoreApplication? I do not want to show this widget, but just want to paint the widget to a png. I need to instantiate the widget, and then call QPixmap::grabWidget().

    I cannot change this from QCoreApplication to QApplication. Are there any other ideas?



  • Hello. There is no way, as I know, to instantiate QWidget in QCoreApplication, but you can grab hidden widget without show that.
    Code and it's execution for example:
    @#include <QtGui>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QLabel *hello = new QLabel("Hello Qt Forum!");
    QPixmap m = QPixmap::grabWidget(hello);
    m.save("C:\Users\1\Desktop\1.png", "png");
    return a.exec();
    }@
    Result:
    !http://s43.radikal.ru/i099/1304/8e/1dd6fcff3b1e.png!



  • I don't think QCoreApplication provides the functionality needed to maintain a GUI. Technically it is possible to draw any widget on an off-screen surface but I don't know if that is possible without a QApplication.



  • Your application uses the Gui module in any case (class QPixmap for example).
    Use the code above, but just do not show widgets.

    bq. Code and it’s execution for example:
    @
    #include <QtGui>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QLabel *hello = new QLabel("Hello Qt Forum!");
    QPixmap m = QPixmap::grabWidget(hello);
    m.save("C:\Users\1\Desktop\1.png", "png");
    return a.exec();
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.