Location acquisition and image operation of each part in QT interface

Posted by cty on Wed, 25 Dec 2019 18:13:11 +0100

1. Introduction to interface location

  • The x(), y(), and pos() functions are all used to get the coordinate position of the upper left corner of the entire form.
  • frameGeometry() corresponds to geometry(). frameGeometry() is to get the upper left vertex and the length and width values of the whole form, while geometry() function is to get the upper left vertex coordinates and the length and width values of the central area of the form.
  • Calling the width() and height() functions directly gets the length and width values of the central area.
  • The results obtained by rect() and size() functions are also for the central area of the form.
    size() gets the length and width of the central area of the window
    rect() and geometry() are the same. They return a QRect object. The length and width values of the two objects are the same, but the vertex coordinates are different. geometry() gets the coordinates of the sitting vertex relative to the parent window, and rect() gets the coordinates of the top left vertex (0, 0).

Example code:

QString xStr;                      	//Get the result of the x() function and display
xValueLabel->setText(xStr.setNum(x()));
QString yStr;                      	//Get the result of the y() function and display
yValueLabel->setText(yStr.setNum(y()));
QString frameStr;                   //Get the result of the frameGeometry() function and display
QString tempStr1,tempStr2,tempStr3,tempStr4;
frameStr = tempStr1.setNum(frameGeometry().x())+","+
           tempStr2.setNum(frameGeometry().y())+","+
           tempStr3.setNum(frameGeometry().width())+","+
           tempStr4.setNum(frameGeometry().height());
FrmValueLabel->setText(frameStr);
QString positionStr;            	//Get the result of the pos() function and display
QString tempStr11,tempStr12;
positionStr =tempStr11.setNum(pos().x())+","+
             tempStr12.setNum(pos().y());
posValueLabel->setText(positionStr);
QString geoStr;               		//Get the result of the geometry() function and display
QString tempStr21,tempStr22,tempStr23,tempStr24;
geoStr =tempStr21.setNum(geometry().x())+","+
        tempStr22.setNum(geometry().y())+","+
        tempStr23.setNum(geometry().width())+","+
        tempStr24.setNum(geometry().height());
geoValueLabel->setText(geoStr);
QString wStr,hStr;                  //Get the results of the width() and height() functions and display
widthValueLabel->setText(wStr.setNum(width()));
heightValueLabel->setText(hStr.setNum(height()));
QString rectStr;                    //Get the result of the rect() function and display
QString tempStr31,tempStr32,tempStr33,tempStr34;
rectStr =tempStr31.setNum(rect().x())+","+
         tempStr32.setNum(rect().y())+","+
         tempStr33.setNum(/*rect().width()*/width())+","+
         tempStr34.setNum(height()/*rect().height()*/);
rectValueLabel->setText(rectStr);
QString sizeStr;                    //Get the result of the size() function and display
QString tempStr41,tempStr42;
sizeStr =tempStr41.setNum(size().width())+","+
         tempStr42.setNum(size().height());
sizeValueLabel->setText(sizeStr);

2. Image loading, scaling, rotation and mirroring
Loading pictures

  • Loading dynamic graph through QMovie

     QLabel *label =new QLabel();
     QMovie *movie =new QMovie("D:/Project/Qt/firstQT/2.gif");
     label->setMovie(movie);
     movie->start();
     label->show();
    
  • Load pictures through QPixmap

     QPixmap pixmap("label.png");
     label->setPixmap(pixmap);
     
     // Also can
     label->setPixmap(QPixmap("label.png"));
     
     label->show();
    
  • Load pictures through QImage

     QImage *image= new QImage("./pic1.jpg"); 
     image->load("./pic2.jpg");                   // Replace loaded pictures
     label->setPixmap(QPixmap::fromImage(image));
     label->show();
    
  • Direct rendering (not studied yet)

Zoom, rotate, and mirror images

if(img.isNull())                                            // Validity judgment
	return;
QMatrix martix;                                             // Declare an instance of QMatrix class
martix.scale(2,2);	                                    // Enlarge by (2:2) and reduce by decimal, for example, (0.5:0.5) means reduce by 1 / 2
matrix.rotate(90);                                          // Rotate 90 degrees
img = img.transformed(martix);// Convert by QMatrix matrix
img=img.mirrored(true,false);// Mirror conversion (true,false) horizontal mirror conversion (false, true) vertical mirror conversion
//Reset display graphics
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));

Topics: Qt