Created
June 21, 2023 05:55
-
-
Save ialexpovad/6aa13532885bff7c774590ffcf8e4c77 to your computer and use it in GitHub Desktop.
Drag and drop with darkened area on qt (c++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "form.h" | |
#include "ui_form.h" | |
#include <QDragEnterEvent> | |
#include <QDragMoveEvent> | |
#include <QDragLeaveEvent> | |
#include <QDropEvent> | |
#include <QMimeData> | |
#include <QUrl> | |
#include <QFile> | |
#include <QXmlStreamReader> | |
#include <QPainter> | |
Form::Form(QWidget *parent) : | |
QWidget(parent), | |
ui(new Ui::Form) | |
{ | |
ui->setupUi(this); | |
setAcceptDrops(true); | |
setAutoFillBackground(true); | |
// // Set the background color to a dark shade | |
// QPalette palette = this->palette(); | |
// palette.setColor(QPalette::Window, QColor(30, 30, 30)); | |
// setPalette(palette); | |
} | |
Form::~Form() | |
{ | |
delete ui; | |
} | |
void Form::dragEnterEvent(QDragEnterEvent *event) | |
{ | |
if (event->mimeData()->hasUrls()) { | |
// Check if any of the dropped URLs have XML file extension | |
for (const QUrl& url : event->mimeData()->urls()) { | |
if (url.isLocalFile() && url.toLocalFile().endsWith(".xml")) { | |
event->acceptProposedAction(); | |
return; | |
} | |
} | |
} | |
} | |
void Form::dropEvent(QDropEvent *event) | |
{ | |
if (event->mimeData()->hasUrls()) { | |
QStringList xmlFiles; | |
// Extract the dropped XML file paths | |
for (const QUrl& url : event->mimeData()->urls()) { | |
if (url.isLocalFile() && url.toLocalFile().endsWith(".xml")) | |
xmlFiles.append(url.toLocalFile()); | |
} | |
// Process the XML files (e.g., parse and display in QPlainTextEdit) | |
for (const QString& file : xmlFiles) { | |
parseXmlFile(file); | |
} | |
event->acceptProposedAction(); | |
} | |
} | |
void Form::dragMoveEvent(QDragMoveEvent* event) { | |
if (event->mimeData()->hasUrls()) | |
event->acceptProposedAction(); | |
} | |
void Form::dragLeaveEvent(QDragLeaveEvent* event) { | |
event->accept(); | |
update(); | |
} | |
void Form::parseXmlFile(const QString& filePath) { | |
QFile file(filePath); | |
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | |
qDebug() << "Failed to open file: " << file.errorString(); | |
return; | |
} | |
QXmlStreamReader xmlReader(&file); | |
while (!xmlReader.atEnd() && !xmlReader.hasError()) { | |
QXmlStreamReader::TokenType token = xmlReader.readNext(); | |
if (token == QXmlStreamReader::StartElement) { | |
xmlContent.append("<" + xmlReader.name().toString() + ">\n"); | |
} else if (token == QXmlStreamReader::EndElement) { | |
xmlContent.append("</" + xmlReader.name().toString() + ">\n"); | |
} else if (token == QXmlStreamReader::Characters) { | |
QString text = xmlReader.text().toString().trimmed(); | |
if (!text.isEmpty()) | |
xmlContent.append(text + "\n"); | |
} | |
} | |
if (xmlReader.hasError()) { | |
qDebug() << "XML parsing error: " << xmlReader.errorString(); | |
return; | |
} | |
emit textsignal(xmlContent); | |
file.close(); | |
} | |
void Form::paintEvent(QPaintEvent* event) | |
{ | |
QWidget::paintEvent(event); | |
// Adjust the background color and transparency based on hover state | |
QPainter painter(this); | |
painter.setRenderHint(QPainter::Antialiasing); | |
QPalette appPalette = QApplication::palette(); | |
// Retrieve the color you're interested in | |
QColor appColor = appPalette.color(QPalette::Window); | |
if (underMouse()) | |
painter.fillRect(rect(), QColor(30, 30, 30, 150)); // Darkened color with transparency | |
else | |
painter.fillRect(rect(), appColor); // Default darkened color | |
// Draw a message text in the center of the darkened area | |
painter.setPen(Qt::black); | |
painter.setFont(QFont("Arial", 14, QFont::Bold)); | |
QRectF textRect = rect().adjusted(10, 10, -10, -10); | |
painter.drawText(textRect, Qt::AlignCenter, "Drag and Drop Files Here"); | |
} | |
bool Form::isHovered(const QPoint& pos) const { | |
return rect().contains(pos); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FORM_H | |
#define FORM_H | |
#include <QWidget> | |
namespace Ui { | |
class Form; | |
} | |
class Form : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
explicit Form(QWidget *parent = nullptr); | |
~Form(); | |
signals: | |
QString textsignal(const QString& str); | |
private: | |
Ui::Form *ui; | |
QString xmlContent; | |
void parseXmlFile(const QString& filePath); | |
bool isHovered(const QPoint& pos) const; | |
protected: | |
void dragEnterEvent(QDragEnterEvent *event) override; | |
void dropEvent(QDropEvent *event) override; | |
void dragMoveEvent(QDragMoveEvent* event) override; | |
void paintEvent(QPaintEvent* event) override; | |
void dragLeaveEvent(QDragLeaveEvent* event) override ; | |
}; | |
#endif // FORM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include <QApplication> | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
MainWindow w; | |
w.show(); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include "form.h" | |
MainWindow::MainWindow(QWidget *parent) | |
: QMainWindow(parent) | |
, ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
Form *w = new Form(ui->widget); | |
QObject::connect(w, &Form::textsignal, [this](QString str) {ui->plainTextEdit->appendPlainText(str);}); | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MAINWINDOW_H | |
#define MAINWINDOW_H | |
#include <QMainWindow> | |
#include <QListWidget> | |
#include <QPlainTextEdit> | |
QT_BEGIN_NAMESPACE | |
namespace Ui { class MainWindow; } | |
QT_END_NAMESPACE | |
class MainWindow : public QMainWindow | |
{ | |
Q_OBJECT | |
public: | |
MainWindow(QWidget *parent = nullptr); | |
~MainWindow(); | |
private: | |
Ui::MainWindow *ui; | |
private: | |
}; | |
#endif // MAINWINDOW_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment