Graph Selection Function Implementation of Qt C omponent QScrollArea

1. Basic Information

Programming language: C++.
Visualization Platform: Qt
Main components: QScrollArea, QLable

2. Key Points

1.QScrollArea

(1) The code for adding pictures with QScrollArea is as follows:

QScrollArea scrollArea;
QLabel imageLabel;
QPixmap pixMap;
imageLabel.setPixmap(pixMap);
scrollArea.setWidget(imageLable);

(2) Conversion of scroll zone coordinates to coordinates in actual pictures

The coordinate conversion formula is QImage(X,Y)=QScroll Value(h,v)+QReal(x,y)

PImage(X,Y) is the actual coordinate of the image, PScroll Value(h,v) is the coordinate of the value() value of horizontal and vertical scrollbars, and PReal(x,y) is the relative coordinate of scrollArea.

Here is a simple example of the relationship between ScrollBar value() and step pageStep() (from Qt Helper):
If we have a document with 100 lines, and we can only show 20 lines in a widget, we may wish to construct a scroll bar with a page step of 20, a minimum value of 0, and a maximum value of 80. This would give us a scroll bar with five "pages".

The relative coordinates of the QLable can be obtained by referring to the cloud crane dancing Qt Get Mouse Position (Absolute Position, Relative Position) One Article

QPoint *qrealPoint;
qrealPoint= event->globalPos(); //Gets the global location, event is the mouse click event
qrealPoint= ui->leftImgLabel>mapFromGlobal(qrealPoint);

The scrollbar value() value is obtained as follows:

QScrollBar *scrollBar_X = scrollArea->horizontalScrollBar();
float x_offset = scrollBar_X->value();
2.Qt Event Filter

The main ways to use event filters are as follows:

The bool eventFilter(QObject *obj, QEvent *eve) is declared in the.h file;
Registered in.cpp constructorUi.sa_GeoImage->installEventFilter (this);
Override eventFilter() method in.cpp

3. Code

/**
 *  @BRIEF Reimplement eventFilter()
 *  @param *obj: Which object we should control
 *  @param *eve: Which event has happened
 */
bool MatchWindowSingle::eventFilter(QObject *obj, QEvent *eve)
{
    if (obj == ui.sa_geoImage){                             //Check whether it's a geoImage Scroll Area
        if (selectPolicy == GEOIMAGE_SELECTING){            //Check whether geoImage can be selected
            if (eve->type() == QEvent::MouseButtonRelease){ //Check mouse event
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(eve);
                if (mouseEvent->button() == Qt::LeftButton){ // Left button click

                    //Get Image Position of the Select Point.
                    Point2f imagePoint = getImageCoordinate(mouseEvent, ui.sa_geoImage, &scaleFactor_g);                

                    //Draw point in the screen.     
                    int pointSize = (int)(2/scaleFactor_MIN_g);
                    int pix_x = (int)imagePoint.x;
                    int pix_y = (int)imagePoint.y;              
                    QPen pen(QColor(250,112,154));              //Set pen
                    QBrush brush(QColor(250,112,154));  //Set brush
                    QPainter painter(&geoPic);      
                    painter.setPen(pen);
                    painter.setBrush(brush);
                    painter.drawEllipse(QPoint(pix_x,pix_y), pointSize, pointSize);
                    painter.setRenderHint(QPainter::Antialiasing);//Make it more smooth 

                    //Update the scrollArea.
                    updateScrollArea(&geoPic, ui.sa_geoImage, &geoImageLable, &scaleFactor_g);
                    selectPolicy = VIDEOIMAGE_SELECTING;
                    return true;
                }else{return false;}
            }else{return false;}
        }else{return false;}        
    }else if (obj == ui.sa_videoImage){                     //Check whether it's a videoImage Scroll Area
        if (selectPolicy == VIDEOIMAGE_SELECTING){          //Check whether videoImage can be selected
            if (eve->type() == QEvent::MouseButtonRelease){ //Check mouse event
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(eve);
                if (mouseEvent->button() == Qt::LeftButton){ // Left button click

                    //Get Image Position of the Select Point.
                    Point2f imagePoint = getImageCoordinate(mouseEvent, ui.sa_videoImage, &scaleFactor_v);              

                    //Draw point in the screen.     
                    int pointSize = (int)(2/scaleFactor_MIN_v);
                    int pix_x = (int)imagePoint.x;
                    int pix_y = (int)imagePoint.y;              
                    QPen pen(QColor(254,225,64));               //Set pen
                    QBrush brush(QColor(254,225,64));   //Set brush
                    QPainter painter(&videoPic);        
                    painter.setPen(pen);
                    painter.setBrush(brush);
                    painter.drawEllipse(QPoint(pix_x,pix_y), pointSize, pointSize);
                    painter.setRenderHint(QPainter::Antialiasing);//Make it more smooth 

                    //Update the scrollArea.
                    updateScrollArea(&videoPic, ui.sa_videoImage, &videoImageLable, &scaleFactor_v);
                    selectPolicy = GEOIMAGE_SELECTING;
                    return true;
                }else{return false;}
            }else{return false;}
        }else{return false;}
    }else{return false;}
}

Tags: Qt Programming

Posted on Sat, 13 Jun 2020 12:58:50 -0400 by Nilanka