gpt4 book ai didi

c++ - 在屏幕上的确切位置保存和恢复 Qt 小部件

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:03 25 4
gpt4 key购买 nike

我一直在尝试使用现有代码来保存和恢复标签,并在未来将 Qt 小部件保存在屏幕上用户放置它的确切位置。我尝试这样做的方法是在 Label 头文件中使用 QPoint position 变量。

到目前为止,保存和恢复代码运行良好。唯一的问题是导入的标签图像保存在左上角的屏幕上。我似乎无法破解保存和恢复用户使用 QPoint position 变量放置它的位置。

label.h

#ifndef LABEL_H
#define LABEL_H

#include "mainwindow.h"

#include <QtGui>
#include <QLabel>
#include <QFileDialog>
#include <QBoxLayout>
#include <QVariant>
#include <QGraphicsItem>
#include <QPoint>

class Label : public QLabel
{
public:
// Constructor
Label();

Label(QWidget* aParentWidget)
: QLabel(aParentWidget)
, m_nMouseClick_X_Coordinate(0)
, m_nMouseClick_Y_Coordinate(0)
{
m_pParentWidget = aParentWidget;
}

~Label();

void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void mouseDoubleClickEvent(QMouseEvent* event);

QPoint position; // Exact location debugged on console

private:
int m_nMouseClick_X_Coordinate;
int m_nMouseClick_Y_Coordinate;
QWidget* m_pParentWidget;
};

#endif // LABEL_H

标签.cpp

#include "label.h"

//---------------------------------------
// Deconstructor
//---------------------------------------
Label::~Label()
{
}

void Label::mousePressEvent(QMouseEvent *event)
{
// Move the coordinates on the main window
m_nMouseClick_X_Coordinate = event->x();
m_nMouseClick_Y_Coordinate = event->y();

// Display coordinates in qDebug
//position = event->pos();

position = event->pos();
//qDebug() << event->pos();
qDebug() << position;
}

void Label::mouseMoveEvent(QMouseEvent *event)
{
//-------------------------------------------------------------
// Allow the user to drag the graphics on the Display
//-------------------------------------------------------------
move(event->globalX()-m_nMouseClick_X_Coordinate-m_pParentWidget->geometry().x(),
event->globalY()-m_nMouseClick_Y_Coordinate-m_pParentWidget->geometry().y());
}

void Label::mouseDoubleClickEvent(QMouseEvent *event)
{
//QByteArray bArray;
//QBuffer buffer(&bArray);
//buffer.open(QIODevice::WriteOnly);

//--------------------------------
// Open file dialog
//--------------------------------
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images(*.png, *.dxf, *.jpg"));
dialog.setViewMode(QFileDialog::Detail);
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Images"),
"/home",
tr("Image Files (*.png *.jpg *.bmp)"));


if (!fileName.isEmpty())
{
QImage image(fileName);
Label::setPixmap(fileName);
Label::adjustSize();
}
}

主窗口.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
readSettings();
ui->setupUi(this);

// Set up the window size
this->setWindowTitle(QString::fromUtf8("Raspberry PI GUI v1.0"));

this->resize(800, 400);

// Add label Button
button = new QPushButton("Add Graphic", this);
button->setGeometry(QRect(QPoint(10, 20), QSize(200, 50)));
button->show();
QObject::connect(button, SIGNAL(pressed()), this, SLOT(input_label()));
}

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

void MainWindow::input_label()
{
Label *label = new Label(this);
label->setText("New Graphic");
label->show();
this->labels.append(label);
}

void MainWindow::writeSettings()
{
// Save location
//https://www.ics.com/designpatterns/book/qsettings.html

int i = 1;
Q_FOREACH(auto label, labels)
{
if (label->pixmap() != nullptr)
{
QByteArray bArray;
QBuffer buffer(&bArray);
buffer.open(QIODevice::WriteOnly);
label->pixmap()->save(&buffer, "PNG");

QSettings settings("Save state", "GUIApp");
settings.beginGroup("MainWindow");
settings.setValue(QString("image-%1").arg(i), bArray);

++i;
}
}
}

void MainWindow::readSettings()
{
QSettings settings("Save state", "GUIApp");

settings.beginGroup("MainWindow");
int i = 1;
while (true)
{
// Restore position
// Need to find a way to find coordinates of x and y from label
QPoint pos = settings.value("pos", QPoint(Label.position)).toPoint();

QByteArray image = settings.value(QString("image-%1").arg(i)).toByteArray();
if (!image.isNull()) {
QPixmap pixmap;
if (pixmap.loadFromData(image))
{
input_label(); // add new label
this->labels.back()->setPixmap(pixmap);
}
} else break;
++i;
}
}

void MainWindow::closeEvent(QCloseEvent *event)
{
writeSettings();
event->accept();
}

最佳答案

您必须记下并阅读该位置才能添加恢复功能。下面的代码只是这个想法的草稿,我还没有对其进行测试,但希望能引导您找到最终的解决方案。

注意:我还进行了一些修改以简化您的代码,因为您有冗余信息使解决方案复杂化。提两个:

  • 我删除了m_nMouseClick_X_Coordinatem_nMouseClick_Y_Coordinateposition,你可以调用内置的pos() 代替

  • 您可以使用 QWidget::parentWidget访问父级


修改后的标签:

class Label : public QLabel
{
public:
// Constructor
Label();

Label(QWidget* aParentWidget)
: QLabel(aParentWidget)
{
// for this solution aParentWidget CANNOT be null
}

~Label();

void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void mouseDoubleClickEvent(QMouseEvent* event);
};

在 .cpp 文件中(为简短起见,删除了明显的代码)

void Label::mousePressEvent(QMouseEvent *event)
{
move(parentWidget()->mapFromGlobal(event->globalPos()));
}

void Label::mouseMoveEvent(QMouseEvent *event)
{
move(parentWidget()->mapFromGlobal(event->globalPos()));
}

void MainWindow::writeSettings() 中:

settings.setValue(QString("image-%1-pos").arg(i), label->pos());

void MainWindow::readSettings() 中:

this->labels.back()->setPixmap(pixmap);
this->labels.back()->move(settings.value(QString("image-%1-pos").arg(i), QPoint(0, 0)).toPoint()); // default position (0, 0)

标签的位置现在将取决于 parent 的位置。如果你想让它完全全局化,将相应的行更改为:

// write
settings.setValue(QString("image-%1-pos").arg(i), mapToGlobal(label->pos()));

// read
this->labels.back()->move(mapFromGlobal(settings.value(QString("image-%1-pos").arg(i)).toPoint()));

恢复标签时可能会出现轻微的闪烁,因为您显示标签后它们会被移动。要修复它,您可以先读取位置,然后将其作为参数传递给创建标签的函数。为了使其与您的按钮兼容,请将其默认值设置为 (0, 0)。

关于c++ - 在屏幕上的确切位置保存和恢复 Qt 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44241506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com