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. Change Desktop Wallpaper
Forum Updated to NodeBB v4.3 + New Features

Change Desktop Wallpaper

Scheduled Pinned Locked Moved General and Desktop
40 Posts 5 Posters 21.0k Views 3 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #23

    Ok. good to know,

    Next its to create a timer object
    http://www.bogotobogo.com/Qt/Qt5_QTimer.php
    and then ( code not syntax checked)

    // draw time on image
    QPixmap pix("D:\\Bilder\\BMW\\3er nachher.jpg");
    QPainter paint(pix);
    QTime time = QTime::currentTime();
    QString text = time.toString("hh:mm");
    paint.drawText(pix.width()-100,pix.height()-20,text);
    

    and off u go :)

    QT-static-prgmQ 1 Reply Last reply
    2
    • mrjjM mrjj

      Ok. good to know,

      Next its to create a timer object
      http://www.bogotobogo.com/Qt/Qt5_QTimer.php
      and then ( code not syntax checked)

      // draw time on image
      QPixmap pix("D:\\Bilder\\BMW\\3er nachher.jpg");
      QPainter paint(pix);
      QTime time = QTime::currentTime();
      QString text = time.toString("hh:mm");
      paint.drawText(pix.width()-100,pix.height()-20,text);
      

      and off u go :)

      QT-static-prgmQ Offline
      QT-static-prgmQ Offline
      QT-static-prgm
      wrote on last edited by
      #24

      @mrjj You saved my time. Thanks for the commands. I hope there are not too much questions tomorrow than i find some time to implement it in the cip-pool :D

      mrjjM 1 Reply Last reply
      0
      • QT-static-prgmQ QT-static-prgm

        @mrjj You saved my time. Thanks for the commands. I hope there are not too much questions tomorrow than i find some time to implement it in the cip-pool :D

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #25

        @QT-static-prgm
        Well its a good cause :)
        Repel against the clock-less desktops!!
        I think it would be the easiest to move all code to the timer object.
        and let it draw on image then set it.
        over and over :)

        A 1 Reply Last reply
        1
        • mrjjM mrjj

          @QT-static-prgm
          Well its a good cause :)
          Repel against the clock-less desktops!!
          I think it would be the easiest to move all code to the timer object.
          and let it draw on image then set it.
          over and over :)

          A Offline
          A Offline
          amahta
          wrote on last edited by
          #26

          I'm glad the problem's solved. What @mrjj just mentioned is definitely much easier. My way was more like an example of using interfaces in Qt. They are of course more complicated but Windows API is changing so I guess you won't lose anything by seeing an interface example!
          Good luck both

          Thou shalt programme
          http://www.amin-ahmadi.com

          mrjjM 1 Reply Last reply
          1
          • A amahta

            I'm glad the problem's solved. What @mrjj just mentioned is definitely much easier. My way was more like an example of using interfaces in Qt. They are of course more complicated but Windows API is changing so I guess you won't lose anything by seeing an interface example!
            Good luck both

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #27

            @QT-static-prgm said:
            Yes, the use of interfaces is much more future
            proof and also interesting to see used,
            I also tried in mingw and it seems to think
            IDesktopWallpaper was iUnknow no matter what i did.
            So I guess Visual Studio is indeed better for win development.

            1 Reply Last reply
            0
            • QT-static-prgmQ Offline
              QT-static-prgmQ Offline
              QT-static-prgm
              wrote on last edited by
              #28

              I tried the code yesterday. Nothing changed. It seams to be a problem of SystemParametersInfo. The function is too powerfull and was blocked. It returns always 0. :( will try the alternative version next

              mrjjM 1 Reply Last reply
              0
              • QT-static-prgmQ QT-static-prgm

                I tried the code yesterday. Nothing changed. It seams to be a problem of SystemParametersInfo. The function is too powerfull and was blocked. It returns always 0. :( will try the alternative version next

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #29

                @QT-static-prgm
                hi have you tried to manually set desktop wallpaper.
                If they removed the watch, they have might have
                taken right to set wallpaper also ?

                QT-static-prgmQ 1 Reply Last reply
                1
                • mrjjM mrjj

                  @QT-static-prgm
                  hi have you tried to manually set desktop wallpaper.
                  If they removed the watch, they have might have
                  taken right to set wallpaper also ?

                  QT-static-prgmQ Offline
                  QT-static-prgmQ Offline
                  QT-static-prgm
                  wrote on last edited by
                  #30

                  @mrjj I can manual change the wallpaper. That's no problem. They removed the clock because the Microsoft right handle doesn't work for them, so everyone who can see the clock and change it, too. Some students did that and that way the whole account synchronisation was broken. Since that the clock was removed.

                  I tried to write the clock on the image at my home computer, but i get some errors for this step, too:
                  QPixmap: Must construct a QGuiApplication before a QPixmap

                  mrjjM 1 Reply Last reply
                  0
                  • QT-static-prgmQ QT-static-prgm

                    @mrjj I can manual change the wallpaper. That's no problem. They removed the clock because the Microsoft right handle doesn't work for them, so everyone who can see the clock and change it, too. Some students did that and that way the whole account synchronisation was broken. Since that the clock was removed.

                    I tried to write the clock on the image at my home computer, but i get some errors for this step, too:
                    QPixmap: Must construct a QGuiApplication before a QPixmap

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #31

                    @QT-static-prgm said:

                    Must construct a QGuiApplication

                    If you do have
                    QApplication app( argc, argv );

                    then you must likely have constructed the Pixmap as global before main.

                    and hence its created before "app"

                    QT-static-prgmQ 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @QT-static-prgm said:

                      Must construct a QGuiApplication

                      If you do have
                      QApplication app( argc, argv );

                      then you must likely have constructed the Pixmap as global before main.

                      and hence its created before "app"

                      QT-static-prgmQ Offline
                      QT-static-prgmQ Offline
                      QT-static-prgm
                      wrote on last edited by
                      #32

                      @mrjj It works now :D (on my home pc)

                      #pragma warning(disable: 4100)
                      #include <QGuiApplication>
                      #include <QString>
                      #include <QDebug>
                      #include <QPixmap>
                      #include <QTime>
                      #include <QPainter>
                      #include <Windows.h>
                      #include "shobjidl.h"
                      
                      inline int setwallpaper(QString filePath)
                      {
                      	return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)filePath.utf16(), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
                      }
                      
                      
                      void setWallpaperAdvanced(QString filePath)
                      {
                      	HRESULT hr = CoInitialize(NULL);
                      	IDesktopWallpaper *pDesktopWallpaper = NULL;
                      	hr = CoCreateInstance(__uuidof(DesktopWallpaper), NULL, CLSCTX_ALL, IID_PPV_ARGS(&pDesktopWallpaper));
                      	if (FAILED(hr))
                      	{
                      		qDebug() << "error";
                      	}
                      	else
                      	{
                      		pDesktopWallpaper->SetWallpaper(NULL, filePath.toStdWString().c_str());
                      	}
                      }
                      
                      void drawTime(QPixmap &image, int posX, int posY, QColor color)
                      {
                      	QPainter painter(&image);
                      	painter.setPen(color);
                      	QFont font;
                      	font.setBold(true);
                      	painter.setFont(font);
                      	QTime time = QTime::currentTime();
                      	QDate date = QDate::currentDate();
                      	QString timeText = time.toString("hh:mm");
                      	QString dateText = date.toString(Qt::DefaultLocaleShortDate);
                      
                      	painter.drawText(posX+15,posY,timeText);
                      	painter.drawText(posX,posY+15,dateText);
                      }
                      
                      
                      int main(int argc, char **argv)
                      {
                      
                      	QGuiApplication a(argc, argv);
                      	QString originalFilePath("D:\\workspaces\\Qt_default\\WallpaperApp\\wallpaper.jpg");
                      	QString manipulatedFilePath("D:\\workspaces\\Qt_default\\WallpaperApp\\wallpaper_temp.jpg");
                      
                      	QTime time;
                      
                      	while(true)
                      	{
                      		time = QTime::currentTime();
                      
                      		if(time.second() == 0)
                      		{
                      			QPixmap image(originalFilePath);
                      			drawTime(image, image.width()-75, image.height()-150, Qt::white);
                      			image.save(manipulatedFilePath);
                      			setWallpaperAdvanced(manipulatedFilePath);
                      			qDebug() << "changed";
                      		}
                      		Sleep(800);
                      	}
                      
                      	return 0;
                      }
                      
                      

                      I was just wondering if there is a good way to stop the program. Maybe a tray icon that has an close option. Any ideas??

                      M 1 Reply Last reply
                      1
                      • QT-static-prgmQ QT-static-prgm

                        @mrjj It works now :D (on my home pc)

                        #pragma warning(disable: 4100)
                        #include <QGuiApplication>
                        #include <QString>
                        #include <QDebug>
                        #include <QPixmap>
                        #include <QTime>
                        #include <QPainter>
                        #include <Windows.h>
                        #include "shobjidl.h"
                        
                        inline int setwallpaper(QString filePath)
                        {
                        	return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)filePath.utf16(), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
                        }
                        
                        
                        void setWallpaperAdvanced(QString filePath)
                        {
                        	HRESULT hr = CoInitialize(NULL);
                        	IDesktopWallpaper *pDesktopWallpaper = NULL;
                        	hr = CoCreateInstance(__uuidof(DesktopWallpaper), NULL, CLSCTX_ALL, IID_PPV_ARGS(&pDesktopWallpaper));
                        	if (FAILED(hr))
                        	{
                        		qDebug() << "error";
                        	}
                        	else
                        	{
                        		pDesktopWallpaper->SetWallpaper(NULL, filePath.toStdWString().c_str());
                        	}
                        }
                        
                        void drawTime(QPixmap &image, int posX, int posY, QColor color)
                        {
                        	QPainter painter(&image);
                        	painter.setPen(color);
                        	QFont font;
                        	font.setBold(true);
                        	painter.setFont(font);
                        	QTime time = QTime::currentTime();
                        	QDate date = QDate::currentDate();
                        	QString timeText = time.toString("hh:mm");
                        	QString dateText = date.toString(Qt::DefaultLocaleShortDate);
                        
                        	painter.drawText(posX+15,posY,timeText);
                        	painter.drawText(posX,posY+15,dateText);
                        }
                        
                        
                        int main(int argc, char **argv)
                        {
                        
                        	QGuiApplication a(argc, argv);
                        	QString originalFilePath("D:\\workspaces\\Qt_default\\WallpaperApp\\wallpaper.jpg");
                        	QString manipulatedFilePath("D:\\workspaces\\Qt_default\\WallpaperApp\\wallpaper_temp.jpg");
                        
                        	QTime time;
                        
                        	while(true)
                        	{
                        		time = QTime::currentTime();
                        
                        		if(time.second() == 0)
                        		{
                        			QPixmap image(originalFilePath);
                        			drawTime(image, image.width()-75, image.height()-150, Qt::white);
                        			image.save(manipulatedFilePath);
                        			setWallpaperAdvanced(manipulatedFilePath);
                        			qDebug() << "changed";
                        		}
                        		Sleep(800);
                        	}
                        
                        	return 0;
                        }
                        
                        

                        I was just wondering if there is a good way to stop the program. Maybe a tray icon that has an close option. Any ideas??

                        M Offline
                        M Offline
                        mKing142
                        wrote on last edited by
                        #33

                        @QT-static-prgm I have tried your code but I got error.
                        error: C2065: 'IDesktopWallpaper' : undeclared identifier
                        error: C2065: 'DesktopWallpaper' : undeclared identifier
                        error: C2065: 'pDesktopWallpaper' : undeclared identifier
                        error: C2660: 'CoCreateInstance' : function does not take 3 arguments

                        beside these I have a question.
                        What is the file shobjidl.h that you have included?

                        mrjjM 1 Reply Last reply
                        0
                        • M mKing142

                          @QT-static-prgm I have tried your code but I got error.
                          error: C2065: 'IDesktopWallpaper' : undeclared identifier
                          error: C2065: 'DesktopWallpaper' : undeclared identifier
                          error: C2065: 'pDesktopWallpaper' : undeclared identifier
                          error: C2660: 'CoCreateInstance' : function does not take 3 arguments

                          beside these I have a question.
                          What is the file shobjidl.h that you have included?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #34

                          @mKing142 said in Change Desktop Wallpaper:

                          shobjidl.h

                          This a native win SDK include
                          https://msdn.microsoft.com/en-us/library/windows/desktop/dd391719(v=vs.85).aspx
                          Its needed to know some of the types.

                          Note that with the mingw compiler it would not compile but with visual studio it did.

                          Note2:
                          You might need
                          LIBS += -lOle32
                          LIBS += User32.lib
                          LIBS += Ole32.lib

                          in the .pro file.

                          M 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @mKing142 said in Change Desktop Wallpaper:

                            shobjidl.h

                            This a native win SDK include
                            https://msdn.microsoft.com/en-us/library/windows/desktop/dd391719(v=vs.85).aspx
                            Its needed to know some of the types.

                            Note that with the mingw compiler it would not compile but with visual studio it did.

                            Note2:
                            You might need
                            LIBS += -lOle32
                            LIBS += User32.lib
                            LIBS += Ole32.lib

                            in the .pro file.

                            M Offline
                            M Offline
                            mKing142
                            wrote on last edited by
                            #35

                            @mrjj Thanks for your quick reply.
                            Now am able to understand about shobjidl.h file.

                            I am using MSVC 2013 compiler.

                            I have added
                            LIBS += Ole32.lib
                            in .pro file but still I am getting the error. But if I comment this by pragma i.e. #pragma comment( lib, "Ole32.lib" ) then its compiling. So I just want to know that do I need to add this file somewhere in project settings also.

                            mrjjM 1 Reply Last reply
                            0
                            • hskoglundH Offline
                              hskoglundH Offline
                              hskoglund
                              wrote on last edited by
                              #36

                              Hi, try:

                              LIBS += -lOle32
                              
                              1 Reply Last reply
                              1
                              • M mKing142

                                @mrjj Thanks for your quick reply.
                                Now am able to understand about shobjidl.h file.

                                I am using MSVC 2013 compiler.

                                I have added
                                LIBS += Ole32.lib
                                in .pro file but still I am getting the error. But if I comment this by pragma i.e. #pragma comment( lib, "Ole32.lib" ) then its compiling. So I just want to know that do I need to add this file somewhere in project settings also.

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #37

                                @mKing142 said in Change Desktop Wallpaper:

                                #pragma comment( lib, "Ole32.lib" )

                                No that should be enough.
                                as far as i know its the same as
                                Configuration Properties -> Linker -> Input -> Additional Dependencies.
                                (and add the dll/lib there)

                                Basically we the linker where all the code that goes with the shobjidl.h are.
                                Normally you would have a cpp file ( the h file just declare ) but in this case its
                                inside a windows DLL ( ole32 it seems) so if it links, then nothing more is needed.

                                M 1 Reply Last reply
                                0
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #38

                                  Thanks @hskoglund
                                  I always fiddle around with the syntax. :)

                                  hskoglundH 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    Thanks @hskoglund
                                    I always fiddle around with the syntax. :)

                                    hskoglundH Offline
                                    hskoglundH Offline
                                    hskoglund
                                    wrote on last edited by
                                    #39

                                    @mrjj Yeah me too (have to look at some of my programs every time to remember it :-)

                                    1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @mKing142 said in Change Desktop Wallpaper:

                                      #pragma comment( lib, "Ole32.lib" )

                                      No that should be enough.
                                      as far as i know its the same as
                                      Configuration Properties -> Linker -> Input -> Additional Dependencies.
                                      (and add the dll/lib there)

                                      Basically we the linker where all the code that goes with the shobjidl.h are.
                                      Normally you would have a cpp file ( the h file just declare ) but in this case its
                                      inside a windows DLL ( ole32 it seems) so if it links, then nothing more is needed.

                                      M Offline
                                      M Offline
                                      mKing142
                                      wrote on last edited by
                                      #40

                                      @mrjj Thanks for your comment. I have added the Olib32 in Lib section and tried it. Now the code is building.
                                      But am getting error in CoCreateInstance. The hex code returned by CoCreateInstance is 0x80040154. I have googled this and came to know that the error is "Class not registered". Now am confused.
                                      Do you have any idea regarding this error?

                                      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