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. when i use "QScreen *screen = QGuiApplication::primaryScreen()" ,i get nullptr

when i use "QScreen *screen = QGuiApplication::primaryScreen()" ,i get nullptr

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.0k 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.
  • _ Offline
    _ Offline
    _hunter
    wrote on last edited by
    #1

    include<iostream>
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QScreen *screen = QGuiApplication::primaryScreen();
    
    QString filePathName = "full-";
    
    filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
    
    filePathName += ".jpg";
    if(screen == nullptr)
    {
        std::cout<<"get nothing"<<std::endl;
    }
    

    }

    i always get a nullptr ,i do not know why.anyone can help me.

    _ J.HilkJ 2 Replies Last reply
    0
    • _ _hunter

      include<iostream>
      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);

      QScreen *screen = QGuiApplication::primaryScreen();
      
      QString filePathName = "full-";
      
      filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
      
      filePathName += ".jpg";
      if(screen == nullptr)
      {
          std::cout<<"get nothing"<<std::endl;
      }
      

      }

      i always get a nullptr ,i do not know why.anyone can help me.

      _ Offline
      _ Offline
      _hunter
      wrote on last edited by
      #2

      @_hunter and the os is ubuntu16.04

      1 Reply Last reply
      0
      • _ _hunter

        include<iostream>
        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);

        QScreen *screen = QGuiApplication::primaryScreen();
        
        QString filePathName = "full-";
        
        filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss-zzz");
        
        filePathName += ".jpg";
        if(screen == nullptr)
        {
            std::cout<<"get nothing"<<std::endl;
        }
        

        }

        i always get a nullptr ,i do not know why.anyone can help me.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @_hunter said in when i use "QScreen *screen = QGuiApplication::primaryScreen()" ,i get nullptr:

        QCoreApplication

        I wild guess on my side:
        QCoreApplication is for applications without GUI, QGuiApplication::primaryScreen is static function, but it would at least requiere QT += gui in the pro file.

        so my suggestion, change QCoreApplication to QGuiApplication.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        _ 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @_hunter said in when i use "QScreen *screen = QGuiApplication::primaryScreen()" ,i get nullptr:

          QCoreApplication

          I wild guess on my side:
          QCoreApplication is for applications without GUI, QGuiApplication::primaryScreen is static function, but it would at least requiere QT += gui in the pro file.

          so my suggestion, change QCoreApplication to QGuiApplication.

          _ Offline
          _ Offline
          _hunter
          wrote on last edited by
          #4

          @J.Hilk thank you,this help me a lot

          aha_1980A 1 Reply Last reply
          1
          • _ _hunter

            @J.Hilk thank you,this help me a lot

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @_hunter If your problem is solved, please mark this topic as SOLVED too.

            Thanks.

            Qt has to stay free or it will die.

            1 Reply Last reply
            0

            • Login

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