title: How to take a screenshot of a bunch of labels and other trivial widgts in Qt and save it to a file at runtime?
body: <p>Alright so i was working on a C++ project for school which required me to create an interactive timetable. So i implemented this in Qt and the implementation turned out to be quite successful.</p>
  
  <p>I am now on the final stage where i want to take a <strong>screenshot of a particular part of the window which contains the timetable and store it as an image.</strong></p>
  
  <p><img src="https://i.stack.imgur.com/ro87L.jpg" alt="Here is a picture of the particular window i have Desgined"></p>
  
  <p>Now the problem is that this is <strong>not a single container</strong>. The entries are actually <strong>labels</strong> and i set attributes and wrote code for each of them individually. So i guess i cant use the Pixmap property to grab the widget or something as there is NO widget.</p>
  
  <p>I need the part inside the border to be captured as an image and i want to store it on my PC as a jpeg file when the generate button is pressed. I know how to do the latter but how do i GET the image and store it?</p>
  
  <p>Here is the code for the particular form/window. Not sure it will help but still..</p>
  
  <pre><code>#include "student_tt.h"
  #include "ui_student_tt.h"
  #include "dialog.h"
  #include "QListWidgetItem"
  #include "QListWidget"
  #include "QLCDNumber"
  #include &lt;QString&gt;
  #include &lt;string&gt;
  #include &lt;QMessageBox&gt;
  
  void Student_TT::checkCredits()
  {
      int cnt=0;
      if(!ui-&gt;m1-&gt;text().contains('-'))
      {
          cnt+=5;
      }
      if(!ui-&gt;m1_2-&gt;text().contains('-'))
      {
          cnt+=3;
      }
      if(!ui-&gt;m1_3-&gt;text().contains('-'))
      {
          cnt+=3;
      }
      if(!ui-&gt;m1_4-&gt;text().contains('-'))
      {
          cnt+=4;
      }
      if(!ui-&gt;m1_5-&gt;text().contains('-'))
      {
          cnt+=2;
      }
      QString x=QString::number(cnt);
      ui-&gt;ccount-&gt;setText(x);
      if(cnt&gt;=10&amp;&amp;cnt&lt;=15)
      {
          ui-&gt;tryx-&gt;setStyleSheet("QLabel {color : green}");
          ui-&gt;gbutton-&gt;setEnabled(1);
      }
      else
      {
          ui-&gt;tryx-&gt;setStyleSheet("QLabel { color : red}");
          ui-&gt;gbutton-&gt;setEnabled(0);
      }
  
  
  
      }
  
      Student_TT::Student_TT(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::Student_TT)
      {
          ui-&gt;setupUi(this);
      }
  
      Student_TT::~Student_TT()
      {
          delete ui;
      }
  
      void Student_TT::on_Student_TT_accepted(QString a,QString b,QString c)
      {
  
          ui-&gt;name-&gt;setText("NAME: "+a);
          ui-&gt;reg-&gt;setText("REG.NO: "+b);
          ui-&gt;stm-&gt;setText("STREAM: "+c);
      }
  
      void Student_TT::on_listWidget_itemClicked(QListWidgetItem *item)
      {
          if(item-&gt;isSelected())
          {
              QString a=ui-&gt;m1-&gt;text();
  
              if(a!=item-&gt;text())
              {
  
                  QString x1=item-&gt;text();
                  ui-&gt;sub1-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1-&gt;setStyleSheet("QLabel {color : deepskyblue; }");
                  ui-&gt;w1-&gt;setStyleSheet("QLabel {color : deepskyblue; }");
                  ui-&gt;f1-&gt;setStyleSheet("QLabel {color : deepskyblue; }");//Setting colours to labels
                  ui-&gt;lab1-&gt;setStyleSheet("QLabel {color : deepskyblue; }");
                  ui-&gt;lab1_2-&gt;setStyleSheet("QLabel {color : deepskyblue; }");
                  ui-&gt;m1-&gt;setText(x1);//Setting text
                  ui-&gt;w1-&gt;setText(x1);
                  ui-&gt;f1-&gt;setText(x1);
                  ui-&gt;lab1-&gt;setText(x1+"L");
                  ui-&gt;lab1_2-&gt;setText(x1+"L");
  
              }
              else
              {
                  ui-&gt;sub1-&gt;setStyleSheet("QLabel {color : black; }");
                  QString x1=ui-&gt;m1_8-&gt;text();//Unused
                  ui-&gt;m1-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;w1-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;f1-&gt;setStyleSheet("QLabel {color : green; }");//Setting colours to labels
                  ui-&gt;lab1-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;lab1_2-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1-&gt;setText(x1);//Setting text
                  ui-&gt;w1-&gt;setText(x1);
                  ui-&gt;f1-&gt;setText(x1);
                  ui-&gt;lab1-&gt;setText(x1);
                  ui-&gt;lab1_2-&gt;setText(x1);
  
              }
          }
          checkCredits();//updating credits
      }
  
  
  
      void Student_TT::on_listWidget_2_itemClicked(QListWidgetItem *item)
      {
          if(item-&gt;isSelected())
          {
              QString a=ui-&gt;m1_2-&gt;text();//Its Physics
  
  
              if(a!=item-&gt;text())
              {
  
                  QString x1=item-&gt;text();
                  ui-&gt;sub2-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1_2-&gt;setStyleSheet("QLabel {color : orange; }");
                  ui-&gt;tu1_2-&gt;setStyleSheet("QLabel {color : orange; }");
                  ui-&gt;th1_2-&gt;setStyleSheet("QLabel {color : orange; }");//Setting colours to labels
                  ui-&gt;lab2-&gt;setStyleSheet("QLabel {color : orange; }");
                  ui-&gt;lab21-&gt;setStyleSheet("QLabel {color : orange; }");
                  ui-&gt;m1_2-&gt;setText(x1);//Setting text
                  ui-&gt;tu1_2-&gt;setText(x1);
                  ui-&gt;th1_2-&gt;setText(x1);
                  ui-&gt;lab2-&gt;setText(x1+"L");
                  ui-&gt;lab21-&gt;setText(x1+"L");
              }
              else
              {
                  QString x1=ui-&gt;m1_8-&gt;text();//Unused
                  ui-&gt;sub2-&gt;setStyleSheet("QLabel {color : black; }");
                  ui-&gt;m1_2-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;tu1_2-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;th1_2-&gt;setStyleSheet("QLabel {color : green; }");//Setting colours to labels
                  ui-&gt;lab2-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;lab21-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1_2-&gt;setText(x1);//Setting text
                  ui-&gt;tu1_2-&gt;setText(x1);
                  ui-&gt;th1_2-&gt;setText(x1);
                  ui-&gt;lab2-&gt;setText(x1);
                  ui-&gt;lab21-&gt;setText(x1);
  
  
              }
          }
      checkCredits();//updating credits
      }
  
  
      void Student_TT::on_listWidget_3_itemClicked(QListWidgetItem *item)
      {
          if(item-&gt;isSelected())
          {
              QString a=ui-&gt;m1_3-&gt;text();//Its Chemistry
  
  
              if(a!=item-&gt;text())
              {
  
                  QString x1=item-&gt;text();
                  ui-&gt;sub3-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1_3-&gt;setStyleSheet("QLabel {color : magenta; }");
                  ui-&gt;w1_3-&gt;setStyleSheet("QLabel {color : magenta; }");
                  ui-&gt;f1_3-&gt;setStyleSheet("QLabel {color : magenta; }");
                  ui-&gt;lab32-&gt;setStyleSheet("QLabel {color : magenta; }");
                  ui-&gt;lab33-&gt;setStyleSheet("QLabel {color : magenta; }");
                  ui-&gt;m1_3-&gt;setText(x1);//Setting text
                  ui-&gt;w1_3-&gt;setText(x1);
                  ui-&gt;f1_3-&gt;setText(x1);
                  ui-&gt;lab32-&gt;setText(x1+"L");
                  ui-&gt;lab33-&gt;setText(x1+"L");
              }
              else
              {
                  QString x1=ui-&gt;m1_8-&gt;text();//Unused
                  ui-&gt;sub3-&gt;setStyleSheet("QLabel {color : black; }");
                  ui-&gt;m1_3-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;w1_3-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;f1_3-&gt;setStyleSheet("QLabel {color : green; }");//Setting colours to labels
                  ui-&gt;lab32-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;lab33-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1_3-&gt;setText(x1);//Setting text
                  ui-&gt;w1_3-&gt;setText(x1);
                  ui-&gt;f1_3-&gt;setText(x1);
                  ui-&gt;lab32-&gt;setText(x1);
                  ui-&gt;lab33-&gt;setText(x1);
  
  
              }
          }
  
          checkCredits();//updating credits
      }
  
  
      void Student_TT::on_listWidget_4_itemClicked(QListWidgetItem *item)
      {
          if(item-&gt;isSelected())
          {
              QString a=ui-&gt;m1_4-&gt;text();//Maths
              if(a!=item-&gt;text())
              {
  
                  QString x1=item-&gt;text();
                  ui-&gt;sub4-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1_4-&gt;setStyleSheet("QLabel {color : darkcyan; }");
                  ui-&gt;tu1_4-&gt;setStyleSheet("QLabel {color : darkcyan; }");
                  ui-&gt;th1_4-&gt;setStyleSheet("QLabel {color :  darkcyan; }");
                  ui-&gt;lab43-&gt;setStyleSheet("QLabel {color :  darkcyan; }");
                  ui-&gt;lab44-&gt;setStyleSheet("QLabel {color :  darkcyan; }");
                  ui-&gt;m1_4-&gt;setText(x1);//Setting text
                  ui-&gt;tu1_4-&gt;setText(x1);
                  ui-&gt;th1_4-&gt;setText(x1);
                  ui-&gt;lab43-&gt;setText(x1+"L");
                  ui-&gt;lab44-&gt;setText(x1+"L");
              }
              else
              {
                  QString x1=ui-&gt;m1_8-&gt;text();//Unused
                  ui-&gt;sub4-&gt;setStyleSheet("QLabel {color : black; }");
                  ui-&gt;m1_4-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;tu1_4-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;th1_4-&gt;setStyleSheet("QLabel {color : green; }");//Setting colours to labels
                  ui-&gt;lab43-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;lab44-&gt;setStyleSheet("QLabel {color : green; }");
                  ui-&gt;m1_4-&gt;setText(x1);//Setting text
                  ui-&gt;tu1_4-&gt;setText(x1);
                  ui-&gt;th1_4-&gt;setText(x1);
                  ui-&gt;lab43-&gt;setText(x1);
                  ui-&gt;lab44-&gt;setText(x1);
  
  
              }
          }
          checkCredits();//Updating Credits
      }
  
  void Student_TT::on_pushButton_clicked()//Information
  {
      QMessageBox::information(this,tr("Information"),tr("1. There will be credits for each course marked in '[]'\n2. Credits may be thought of as 'weightage'. More the credits, more important the subject will be.\n3. You must select a minimum of 10 credits and a maximum of 15 credits\n4. To select or change your course, just CLICK on the Course Code"));
  }
  
  void Student_TT::on_listWidget_5_itemClicked(QListWidgetItem *item)
  {
      if(item-&gt;isSelected())
      {
          QString a=ui-&gt;m1_5-&gt;text();//Maths
          if(a!=item-&gt;text())
          {
  
              QString x1=item-&gt;text();
              ui-&gt;sub5-&gt;setStyleSheet("QLabel {color : green; }");
              ui-&gt;m1_5-&gt;setStyleSheet("QLabel {color : lime; }");
              ui-&gt;f1_5-&gt;setStyleSheet("QLabel {color : lime; }");
              ui-&gt;m1_5-&gt;setText(x1);//Setting text
              ui-&gt;f1_5-&gt;setText(x1);
          }
          else
          {
              QString x1=ui-&gt;m1_8-&gt;text();//Unused
              ui-&gt;sub5-&gt;setStyleSheet("QLabel {color : black;}");
              ui-&gt;m1_5-&gt;setStyleSheet("QLabel {color : green;}");
              ui-&gt;f1_5-&gt;setStyleSheet("QLabel {color : green;}");
              ui-&gt;m1_5-&gt;setText(x1);//Setting text
              ui-&gt;f1_5-&gt;setText(x1);
          }
      }
      checkCredits();//Updating Credits
  }
  </code></pre>
  
  <p>Notice that there are a a few componenets from other windows in the code but i guess the code doesn't really need to be understood. You can check each label being assigned colours in the 5 'ItemSelected' functions.</p>