需要在qlist中存储多个矩形并将它们全部显示在QPixmap上

时间:2015-12-09 20:56:33

标签: c++ qt

我已经尝试了很长一段时间了。我需要在支持QRect的Qlabel上绘制多个矩形,我尝试存储QRectF但是在下一个事件之后它取代了矩形并且只显示了一个矩形。 我的代码如下: penlayer.h  class PictureLayer:public QLabel         {             Q_OBJECT             float adjustX;             float adjustY;

        int setRed;
        int setGreen;
        int setBlue;
        int startPointX;
        int finalPointX;
        int startPointY;
        int finalPointY;

        QString imagePath;

    public:
        int x;
        int y;
        QPixmap showPicture;
        QImage imageSource;
        QPoint originPoint;
        QPoint finalPoint;
           QRectF rectColorFrame;

        explicit PictureLayer(QWidget *parent = 0);
        ~PictureLayer(){}

        void leaveEvent(QEvent *);
        void mousePressEvent(QMouseEvent *e);
        void mouseReleaseEvent (QMouseEvent *e);
        void mouseMoveEvent(QMouseEvent *e);
        void imageManipulation(QPoint , QPoint);
    //    void /*paintEvent*/(QPaintEvent *);

    signals:
            void mouse_pressed();
            void mouse_release();
            void mouse_pos();
            void mouse_left();

    private:

    };

和...

    #include "penlayer.h"

    PictureLayer::PictureLayer(QWidget *parent) :
        QLabel (parent)
    {
        adjustX     = 0.0;
        adjustY     = 0.0;
        setRed      = 0;
        setBlue     = 0;
        setGreen    = 0;
        startPointX = 0;
        startPointY = 0;
        finalPointY = 0;
        finalPointX = 0;
        x =0;
        y =0;

    //    Source_Path =":/new/prefix1/testpic/just.jpg";
    //    Source_Path =":/testpic/tst.jpg";
        imagePath =":/testpic/sed.JPG";

        imageManipulation(QPoint (0,0), QPoint (0,0));
    }
    PenLayer::PenLayer(QWidget *parent) :
        QLabel (parent)
    {
        x = 0;
        y = 0;
        rectIntNumber = 0;

        storeRectanglePoints (QPoint (0,0) , QPoint (0,0), 0);

    }//constructor

    /********************Mouse Events********************/

    void PenLayer::mouseMoveEvent (QMouseEvent *e)
    {
        this->x = e->x();
        this->y = e->y();

        emit mouse_pos(/*Look for an event to hold*/);
    }///mouseMoveEvent

    void PenLayer::mousePressEvent(QMouseEvent *e)
    {
        this-> x = e->x ();
        this-> y = e->y ();
        emit mouse_pressed ();

    }///mousePressEvent

    void PenLayer::mouseReleaseEvent (QMouseEvent *e)
    {
        this-> x = e->x ();
        this-> y = e->y ();

        emit mouse_release();
    }///mouseReleaseEvent

    void PenLayer::leaveEvent(QEvent *)
    {
        emit mouse_left();
    }///leaveEvent


    /*************************Setting  rectangle***************************/
    void PenLayer::storeRectanglePoints (QPoint p1 , QPoint p2, int num)
    {
        rectIntNumber = num;
        pressPoint = p1;
        releasePoint = p2;

        pressPair.first = pressPoint.rx ();
        pressPair.second = pressPoint.ry ();
        listOfPoints.insert (rectIntNumber, pressPair);

        releasePair.first = releasePoint.rx ();
        releasePair.second = releasePoint.ry ();
        listOfPoints.insert (rectIntNumber, releasePair);


    //   rect_storeRectanglePoints.setRect (pressPair.first, pressPair.second,
    //                                       releasePair.first, releasePair.second);


    //   rect.insert(rectIntNumber, &rect_storeRectanglePoints);
       qDebug()<<rect;


    }///setRectanglePoints

    /*************************PaintEvent  rectangle***************************/
    void PenLayer::paintEvent(QPaintEvent *pa)
    {
        QPainter painter(this);

        QRectF rectangle( pressPoint , releasePoint );

    //    QPair to QPoint

        if(pressPoint != QPoint(0,0))
        {
            painter.setPen(QPen(Qt::red, 3.0,
                                Qt::DotLine,
                                Qt::SquareCap,
                                Qt::MiterJoin));
            pa->accept ();
        }
        else
        painter.setPen (QPen(Qt::transparent));
    //    painter.drawRects(&rectangle,rectIntNumber);
        //Should draw many rectangles, but wont

    //    qDebug()<<"painter test: clickAction->"<<clickAction;
        painter.drawRect(/**rect.last ()*/rectangle);
        //QPainter should be in the qlist

    //    painter.end ();
    //    onColorRect.insert (rectIntNumber, &painter);
    //    qDebug()<<onColorRect;

    }///paintEvent

dialogfile

    #include "dialog.h"
    #include "uidialog.h"
    #include "picturelayer.h"
    #include "penlayer.h"

    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {

        ui->setupUi(this);
        rectangleNumber = 0;

    /*************************CONNECT STATEMENTS***************************/
        connect(ui-> selectToolLabel,   SIGNAL(mouse_pos()),
                                this,   SLOT(mouse_current_pos()));

        connect(ui-> selectToolLabel,   SIGNAL(mouse_pressed()),
                                this,   SLOT(mouse_pressed()));

        connect(ui-> selectToolLabel,   SIGNAL(mouse_release()),
                                this,   SLOT(mouse_release()));

        connect(ui-> selectToolLabel,   SIGNAL(mouse_left()),
                                this,   SLOT(mouse_left()));

        connect(ui->NewSelection,       SIGNAL(pressed()),
                                this,   SLOT(Button_New()));

        connect(ui->ClearSelection,     SIGNAL(pressed()),
                                this,   SLOT(Button_clear()));

    }

    Dialog::~Dialog()
    {
        delete ui;
    }//deCon

    /*************************Action Buttons*******************************/
    void Dialog::Button_New()
    {
        //- QList to store the paint obj
        //-change pix color from p1 x p2 of rectangleNumber
            //for loop to run a qlist


        //-paint in rectangle

    }

    void Dialog::Button_clear()
    {
        //deletes all stored elements
        //load originalpic

        rectangleNumber = 0;

        ui->selectToolLabel->storeRectanglePoints  ( QPoint (0,0) , QPoint(0,0) ,
                                          rectangleNumber );

        ui->pictureLabel->imageManipulation( QPoint (0,0) , QPoint(0,0) );

        ui->selectToolLabel->listOfPoints.clear ();
    }

    /***********************Mouse Location********************************/

    void Dialog::mouse_current_pos ()
    {
        p3.setX (ui->selectToolLabel->x);
        p3.setY (ui->selectToolLabel->y);

    //    ui->BoxSel->setRectanglePoints (p1,p3,Rectangle_Number);

        ui->selectToolLabel->setCursor (Qt::CrossCursor);
        ui->mouseCurrentPos->setText(QString
                    (" X = %1 , Y = %2")
                        .arg(p3.rx ())  //%1
                        .arg(p3.ry ()));//%2
        update();

    }//mouse_current_pos()


    void Dialog::mouse_pressed()
    {

        //this part should be in QList with a qpair
        //store it in a value and start another array for the new set
        p1.setX (ui->selectToolLabel->x);
        p1.setY (ui->selectToolLabel->y);

        //FIXME: supposed to Draw the temp rectangle

        ui->mouseCurrentEvent->setText(QString
                     ("Mouse pressed at location %1 and %2!!!")
                        .arg(p1.rx ())  //%1
                        .arg(p1.ry ()));//%2
        update();

                                         qDebug()<<"\n Mouse key pressed \n@"
                                             <<p1.rx ()<<p1.ry ()<<rectangleNumber;
    }//mouse_pressed()


    void Dialog::mouse_release()
    {
            rectangleNumber++;
        p2.setX (ui->selectToolLabel->x);
        p2.setY (ui->selectToolLabel->y);

        ui->selectToolLabel->storeRectanglePoints ( p1 , p2 , rectangleNumber );
        //TODO:show rectangle p1 * p2, RectangleNumber++


        ui->pictureLabel->imageManipulation ( p1 , p2);//button new
        ui->mouseCurrentEvent->setText(QString
                    ("Mouse released at location %1 and %2!!!")
                        .arg(p2.rx ())   //%1
                        .arg(p2.ry ())); //%2

        //save and load save

    //    update();
                                            qDebug()<<"test n = "<<rectangleNumber;

    }//mouse_release()


    void Dialog::mouse_left()
    {
        ui->mouseCurrentEvent->setText("Mouse Left :( !!");

                                          qDebug()<<"Mouse is off region \n";
    }//mouse_left()

1 个答案:

答案 0 :(得分:0)

paintEvent()总是从干净的画布开始,似乎只调用drawRect()一次。要保留所有矩形并添加另一个矩形,请将所有rects存储在列表中。在每个paintEvent()中,您将不得不遍历列表并再次绘制所有这些内容。

有很多rects,这可能会导致性能问题。在这种情况下,您还可以选择绘制到QPixmap作为类变量。在将另一个矩形绘制到像素图之后,您只需使用this将像素图绘制到drawPixmap()