UI中的动态QLabel错位

时间:2017-11-13 14:21:14

标签: c++ qt

我正在尝试创建一个通过拖放接受图像并在我的UI上显示这些图像的程序,然后我想重命名并保存它们。

我得到拖放工作,但我的图像放置有一些问题,我似乎无法找到我犯错的地方。

在图像中,您可以在运行时看到我的UI,在左上角,您可以看到我拖入绿色区域的图像的一部分(这是我接受图像的拖放区域)。我实际想要它的位置应该是红色方块。绿色区域是一个名为Imagehandler的动态创建对象,我创建该对象来处理图像的拖放。 Red square是我自己的继承自QLabel的类,我称之为myiconclass。该类应保存实际的图像数据。

我认为我的错误与布局有关,但我看不到它。

请问我能得到一些帮助吗?

enter image description here

Imagehandler.h

#ifndef IMAGEHANDLER_H
#define IMAGEHANDLER_H

#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QList>
#include <QDebug>

//this class is designed to help me take in the images with drag and drop

class ImageHandler : public QWidget
{
    Q_OBJECT
public:
    explicit ImageHandler(QWidget *parent = nullptr);




    QList<QImage> getImageListMemory() const;
    void setImageListMemory(const QList<QImage> &value);

    QList<QUrl> getUrlsMemory() const;
    void setUrlsMemory(const QList<QUrl> &value);

private:
    //QWidget Icon;
    QLabel Icon;
    QList <QImage> imageListMemory;
    QList <QUrl> urlsMemory;

protected:
    void dragEnterEvent(QDragEnterEvent * event);
    void dragLeaveEvent(QDragLeaveEvent * event);
    void dragMoveEvent(QDragMoveEvent * event);
    void dropEvent(QDropEvent * event);

signals:
    void transferImageSignal(QList <QImage>);

public slots:
};

#endif // IMAGEHANDLER_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QImageReader>
#include <QList>
#include <QWidget>
#include <QLabel>
#include <myiconclass.h>
#include <imagehandler.h>
#include <QGridLayout>
#include <QDebug>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QList<QImage> getImageListMemory() const;
    void setImageListMemory(const QList<QImage> &value);

private:
    Ui::MainWindow *ui;
    QLabel Icon;
    QList <QImage> imageListMemory;
    QList <QUrl> urlsMemory;
    QList<QWidget *> labelList;
    ImageHandler * ImageHandlerMemory;
    QGridLayout * grid2;
    QList <MyIconClass *> memory;


signals:

public slots:
    void setIconSlot(QList <QImage>);


};

#endif // MAINWINDOW_H

myiconclass.h

#ifndef MYICONCLASS_H
#define MYICONCLASS_H

#include <QWidget>
#include <QLabel>

//this class is based on a Qlabel and is only made so it can help me with the actual images, gives me more members if I need it

class MyIconClass : public QLabel
{
    Q_OBJECT
public:
    explicit MyIconClass(QWidget *parent = nullptr);

    int getMyNumber() const;
    void setMyNumber(int value);

private:
    int myNumber;

signals:

public slots:
};

#endif // MYICONCLASS_H

imagehandler.cpp

#include "imagehandler.h"

ImageHandler::ImageHandler(QWidget *parent) : QWidget(parent)
{
    setAcceptDrops(true);
}

QList<QImage> ImageHandler::getImageListMemory() const
{
    return imageListMemory;
}

void ImageHandler::setImageListMemory(const QList<QImage> &value)
{
    imageListMemory = value;
}

QList<QUrl> ImageHandler::getUrlsMemory() const
{
    return urlsMemory;
}

void ImageHandler::setUrlsMemory(const QList<QUrl> &value)
{
    urlsMemory = value;
}



void ImageHandler::dragEnterEvent(QDragEnterEvent * event)
{
    event->accept();
}

void ImageHandler::dragLeaveEvent(QDragLeaveEvent * event)
{
    event->accept();
}

void ImageHandler::dragMoveEvent(QDragMoveEvent * event)
{
    event->accept();
}

void ImageHandler::dropEvent(QDropEvent * event)
{
    QList <QImage> imageList2;

    QList <QUrl> urls;
    QList <QUrl>::iterator i;
    urls = event->mimeData()->urls();

    //imageList.append(event->mimeData()->imageData());

    foreach (const QUrl &url, event->mimeData()->urls())
    {
            QString fileName = url.toLocalFile();
            qDebug() << "Dropped file:" << fileName;
            qDebug()<<url.toString();


            QImage img;
            if(img.load(fileName))
            {
                imageList2.append(img);
            }



    }


  emit transferImageSignal(imageList2);

    this->setUrlsMemory(urls);
    this->setImageListMemory(imageList2);


}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ImageHandler * handler = new ImageHandler(this);
    handler->show();
    QGridLayout *grid = new QGridLayout;

    grid->addWidget(handler, 0, 0);

    ui->groupBoxIcon->setLayout(grid);

    ImageHandlerMemory = handler;



    //connect(handler,SIGNAL(handler->transferImageSignal(QList <QUrl>)),this,SLOT(setIconSlot(QList <QUrl>)));
    connect(handler,SIGNAL(transferImageSignal(QList<QImage>)),this,SLOT(setIconSlot(QList<QImage>)));


}

MainWindow::~MainWindow()
{
    delete ui;
}

QList<QImage> MainWindow::getImageListMemory() const
{
    return imageListMemory;
}

void MainWindow::setImageListMemory(const QList<QImage> &value)
{
    imageListMemory = value;
}



void MainWindow::setIconSlot(QList<QImage> images)
{
    printf("succes!");
    this->setImageListMemory(images); //save the images to memory

    QGridLayout *grid = new QGridLayout; //create the grid layout I want my images to be in
    // create counters to remember the row and column in the grid
    int counterRow =0;
    int counterColumn =0;
    int counter3 =0;
    int counterImages = 0;

    //iterate over each image in the list
    QList <QImage>::iterator x;
    for(x = imageListMemory.begin(); x != imageListMemory.end(); x++)
    {
        MyIconClass * myLabel = new MyIconClass(this); //create an object of my own class (which is a Qlabel with an int member)
        QPixmap pixmap(QPixmap::fromImage(*x)); //create a pixmap from the image in the iteration
        myLabel->setPixmap(pixmap); //set the pixmap on my label object
        myLabel->show();
        memory.append(myLabel); //add it to the memory so I can recal it
        counterImages++;


    }

    while(counter3 < images.count())
    {
        grid2->addWidget(memory.value(counter3), counterRow, counterColumn);

        counterColumn++;
        counter3++;
        if(counterColumn >= 5)
        {
            counterRow++;
            counterColumn =0;
        }

    }
    if(ImageHandlerMemory->layout() == 0)
    {
        ImageHandlerMemory->setLayout(grid2);    
    }

}

myiconclass.cpp

#include "myiconclass.h"

MyIconClass::MyIconClass(QWidget *parent) :  QLabel(parent)
{

}

int MyIconClass::getMyNumber() const
{
    return myNumber;
}

void MyIconClass::setMyNumber(int value)
{
    myNumber = value;
}

1 个答案:

答案 0 :(得分:0)

正如本杰明T所说,我必须改变这一点:

MyIconClass * myLabel = new MyIconClass(this);

进入这个:

MyIconClass * myLabel = new MyIconClass(ImageHandlerMemory);

谢谢本杰明!

PS,我还必须在mainwindow构造函数中添加这一行:

grid2 = new QGridLayout;