全部博文(584)
分类:
2011-01-17 14:00:11
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
- protected:
- void dragEnterEvent(QDragEnterEvent *event);
- void dropEvent(QDropEvent *event);
- private:
- bool readFile(const QString &fileName);
- QTextEdit *textEdit;
- };
- #endif // MAINWINDOW_H
- #include "mainwindow.h"
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- textEdit = new QTextEdit;
- setCentralWidget(textEdit);
- textEdit->setAcceptDrops(false);
- setAcceptDrops(true);
- setWindowTitle(tr("Text Editor"));
- }
- MainWindow::~MainWindow()
- {
- }
- void MainWindow::dragEnterEvent(QDragEnterEvent *event)
- {
- if (event->mimeData()->hasFormat("text/uri-list")) {
- event->acceptProposedAction();
- }
- }
- void MainWindow::dropEvent(QDropEvent *event)
- {
- QList
urls = event->mimeData()->urls(); - if (urls.isEmpty()) {
- return;
- }
- QString fileName = urls.first().toLocalFile();
- if (fileName.isEmpty()) {
- return;
- }
- if (readFile(fileName)) {
- setWindowTitle(tr("%1 - %2").arg(fileName, tr("Drag File")));
- }
- }
- bool MainWindow::readFile(const QString &fileName)
- {
- bool r = false;
- QFile file(fileName);
- QTextStream in(&file);
- QString content;
- if(file.open(QIODevice::ReadOnly)) {
- in >> content;
- r = true;
- }
- textEdit->setText(content);
- return r;
- }
- #include
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
本文出自 “豆子空间” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/280052