Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why this program crashed?
Forum Updated to NodeBB v4.3 + New Features

Why this program crashed?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 751 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Van
    wrote on last edited by John Van
    #1

    Hi
    I encountered a problem that confused me. When I debug the program,it crashed.
    2023-06-16_105044.jpg

    int main(int argc, char* argv[])
    {
        QCoreApplication a(argc, argv);
        QImage dfs("1.png");
        QPainter painter;
        bool ret = painter.begin(&dfs);
        auto ddd = dfs.paintEngine();
        if (!dfs.isNull() && painter.isActive())
            painter.drawText(100, 100, QString("1"));
        return a.exec();
    }
    
    

    the qt version is 5.14.2.

    Regrads!

    jsulmJ 1 Reply Last reply
    0
    • J John Van

      Hi
      I encountered a problem that confused me. When I debug the program,it crashed.
      2023-06-16_105044.jpg

      int main(int argc, char* argv[])
      {
          QCoreApplication a(argc, argv);
          QImage dfs("1.png");
          QPainter painter;
          bool ret = painter.begin(&dfs);
          auto ddd = dfs.paintEngine();
          if (!dfs.isNull() && painter.isActive())
              painter.drawText(100, 100, QString("1"));
          return a.exec();
      }
      
      

      the qt version is 5.14.2.

      Regrads!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @John-Van said in Why this program crashed?:

      bool ret = painter.begin(&dfs);

      You do not check whether the image is null or not before you call begin(). I guess it is null because you use a relative path to the image so it is not found...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply
      3
      • jsulmJ jsulm

        @John-Van said in Why this program crashed?:

        bool ret = painter.begin(&dfs);

        You do not check whether the image is null or not before you call begin(). I guess it is null because you use a relative path to the image so it is not found...

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #3

        @jsulm The QImage object exists (it's on the stack) but it may represent a "null" image for the reason you gave. The attempt to paint is guarded by a check for a "null" image, so this should not even be executed in that circumstance.

        @John-Van Crashes here for me with an existing image (Qt 5 or 6).
        The program is crashing because it needs the QFontDatabase to render text. The requires at least a QGuiApplication object. Under Qt 6 you get the useful warning QFontDatabase: Must construct a QGuiApplication before accessing QFontDatabase before the crash.

        jsulmJ J 2 Replies Last reply
        2
        • C ChrisW67

          @jsulm The QImage object exists (it's on the stack) but it may represent a "null" image for the reason you gave. The attempt to paint is guarded by a check for a "null" image, so this should not even be executed in that circumstance.

          @John-Van Crashes here for me with an existing image (Qt 5 or 6).
          The program is crashing because it needs the QFontDatabase to render text. The requires at least a QGuiApplication object. Under Qt 6 you get the useful warning QFontDatabase: Must construct a QGuiApplication before accessing QFontDatabase before the crash.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ChrisW67 said in Why this program crashed?:

          but it may represent a "null" image for the reason you gave

          That's what I was talking about

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C ChrisW67

            @jsulm The QImage object exists (it's on the stack) but it may represent a "null" image for the reason you gave. The attempt to paint is guarded by a check for a "null" image, so this should not even be executed in that circumstance.

            @John-Van Crashes here for me with an existing image (Qt 5 or 6).
            The program is crashing because it needs the QFontDatabase to render text. The requires at least a QGuiApplication object. Under Qt 6 you get the useful warning QFontDatabase: Must construct a QGuiApplication before accessing QFontDatabase before the crash.

            J Offline
            J Offline
            John Van
            wrote on last edited by
            #5

            @ChrisW67 Thank you.The problem was resolved after I used QApplication instead of QCoreApplication.

            1 Reply Last reply
            0
            • J John Van has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved