Qt拖放操作和打印操作的實現(xiàn)_第1頁
Qt拖放操作和打印操作的實現(xiàn)_第2頁
Qt拖放操作和打印操作的實現(xiàn)_第3頁
Qt拖放操作和打印操作的實現(xiàn)_第4頁
Qt拖放操作和打印操作的實現(xiàn)_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第Qt拖放操作和打印操作的實現(xiàn)目錄1.拖放操作1.1使用拖放打開文件(拖動.txt文件)1.2自定義拖放操作(拖動圖片)2.打印操作3.資源下載

1.拖放操作

拖放操作分為拖動(Drag)和放下(Drop)兩種操作,當數(shù)據(jù)拖動時會被存儲為MIME(MultipurposeInternetMailExtensions)類型。

1.1使用拖放打開文件(拖動.txt文件)

下面完成將.txt文件拖如界面里的操作。

頭文件函數(shù)聲明

voiddragEnterEvent(QDragEnterEvent*event);//拖動進入事件

voiddropEvent(QDropEvent*event);//放下事件

源文件函數(shù)實現(xiàn)

#include"mainwindow.h"

#include"ui_mainwindow.h"

#includeQDragEnterEvent

#includeQUrl

#includeQFile

#includeQTextStream

#includeQMimeData

MainWindow::MainWindow(QWidget*parent):

QMainWindow(parent),

ui(newUi::MainWindow)

ui-setupUi(this);

setAcceptDrops(true);//假如這行代碼后,主窗口就可以接收放下事件了

MainWindow::~MainWindow()

deleteui;

//拖動進入事件

voidMainWindow::dragEnterEvent(QDragEnterEvent*event)

if(event-mimeData()-hasUrls())//數(shù)據(jù)中是否包含URL

event-acceptProposedAction();//接收

else

event-ignore();//忽略

//放下事件

voidMainWindow::dropEvent(QDropEvent*event)

constQMimeData*mimeData=event-mimeData();//獲取MIME數(shù)據(jù)

if(mimeData-hasUrls())

QListQUrlurlList=mimeData-urls();

QStringfileName=urlList.at(0).toLocalFile();

if(!fileName.isEmpty())

QFilefile(fileName);//建立QFile對象,以只讀方式打開文件

if(!file.open(QIODevice::ReadOnly))

return;

QTextStreamin(file);//建立文本流對象

ui-textEdit-setText(in.readAll());

常用MIME類型數(shù)據(jù)處理函數(shù)

1.2自定義拖放操作(拖動圖片)

在界面上任意拖動這張圖片

圖片原來的位置蒙上黑色陰影效果

頭文件函數(shù)聲明

protected:

voidmousePressEvent(QMouseEvent*event);//鼠標按下事件

voiddragEnterEvent(QDragEnterEvent*event);//拖動進入事件

voiddragMoveEvent(QDragMoveEvent*event);//拖動事件

voiddropEvent(QDropEvent*event);//放下事件

編輯源文件

#include"mainwindow.h"

#includeQLabel

#include"ui_mainwindow.h"

#includeQMouseEvent

#includeQDrag

#includeQDragEnterEvent

#includeQDragMoveEvent

#includeQDropEvent

#includeQPainter

#includeQMimeData

MainWindow::MainWindow(QWidget*parent):

QMainWindow(parent),

ui(newUi::MainWindow)

ui-setupUi(this);

setAcceptDrops(true);//設(shè)置窗口部件可以接收拖入

QLabel*label=newQLabel(this);

QPixmappix("..//imagedragdrop/logo.png");

label-setPixmap(pix);

label-resize(pix.size());

label-move(100,100);

label-setAttribute(Qt::WA_DeleteOnClose);//當窗口關(guān)閉時銷毀圖片

MainWindow::~MainWindow()

deleteui;

//鼠標按下事件

voidMainWindow::mousePressEvent(QMouseEvent*event)

//第1步:獲取圖片

//將鼠標指針所在位置的部件強制轉(zhuǎn)換為Label類型

QLabel*child=static_castQLabel*(childAt(event-pos()));

if(!child-inherits("QLabel"))

return;

QPixmappixmap=*child-pixmap();

//第2步:自定義MIME類型

QByteArrayitemData;

QDataStreamdataStream(itemData,QIODevice::WriteOnly);

//將圖片信息,位置信息輸入到自己數(shù)組中

dataStreampixmapQPoint(event-pos()-child-pos());

//第3步:將數(shù)據(jù)放入QMimeData中

QMimeData*mimeData=newQMimeData;

//將字節(jié)數(shù)組放入QMimeData中,這里的MIME類型是我們自己定義的

mimeData-setData("myimage/png",itemData);

//第4步:將QMimeData數(shù)據(jù)放入QDrag中

QDrag*drag=newQDrag(this);//創(chuàng)建QDrag用來移動數(shù)據(jù)

drag-setMimeData(mimeData);

drag-setPixmap(pixmap);//在移動過程中顯示圖片,若不設(shè)置則默認顯示一個小矩形

drag-setHotSpot(event-pos()-child-pos());//拖動時鼠標指針的位置不變

//第5步:給原圖添加陰影

QPixmaptempPixmap=pixmap;

QPainterpainter;

painter.begin(tempPixmap);

//在圖片的外接矩形中添加一層透明的淡黑色形成陰影效果

painter.fillRect(pixmap.rect(),QColor(127,127,127,127));

painter.end();

child-setPixmap(tempPixmap);//在移動圖片過程中,讓原圖片有一層黑色陰影

//第6步:執(zhí)行拖放操作

if(drag-exec(Qt::CopyAction|Qt::MoveAction,Qt::CopyAction)==Qt::MoveAction)

child-close();//如果是移動操作,那么拖放完成后關(guān)閉原標簽

else

child-show();//如果是復(fù)制操作,那么拖放完成后顯示標簽

child-setPixmap(pixmap);//顯示原圖片,不再使用陰影

//拖入事件

voidMainWindow::dragEnterEvent(QDragEnterEvent*event)

if(event-mimeData()-hasFormat("myimage/png"))

event-setDropAction(Qt::MoveAction);

event-accept();

else

event-ignore();

//拖動事件

voidMainWindow::dragMoveEvent(QDragMoveEvent*event)

if(event-mimeData()-hasFormat("myimage/png"))

event-setDropAction(Qt::MoveAction);

event-accept();

else

event-ignore();

//放下事件

voidMainWindow::dropEvent(QDropEvent*event)

if(event-mimeData()-hasFormat("myimage/png"))

QByteArrayitemData=event-mimeData()-data("myimage/png");

QDataStreamdataStream(itemData,QIODevice::ReadOnly);

QPixmappixmap;

QPointoffset;

dataStreampixmapoffset;

QLabel*newLabel=newQLabel(this);

newLabel-setPixmap(pixmap);

newLabel-resize(pixmap.size());

newLabel-move(event-pos()-offset);

newLabel-show();

newLabel-setAttribute(Qt::WA_DeleteOnClose);//當窗口關(guān)閉時銷毀圖片

event-setDropAction(Qt::MoveAction);

event-accept();

else

event-ignore();

2.打印操作

QtPrintSupport模塊提供了對打印的支持。只需要使用一個QPrinter類和一個QPrintDialog類就可以完成文檔的打印操作。

文件

QT+=printsupport

頭文件

#ifndefMAINWINDOW_H

#defineMAINWINDOW_H

#includeQMainWindow

classQPrinter;

namespaceUi{

classMainWindow;

classMainWindow:publicQMainWindow

Q_OBJECT

public:

explicitMainWindow(QWidget*parent=0);

~MainWindow();

private:

Ui::MainWindow*ui;

privateslots:

voiddoPrint();

voiddoPrintPreview();

voidprintPreview(QPrinter*printer);

voidcreatePdf();

#endif//MAINWINDOW_H

源文件

#include"mainwindow.h"

#include"ui_mainwindow.h"

#includeQPrinter

#includeQPrintDialog

#includeQPrintPreviewDialog

#includeQFileDialog

#includeQFileInfo

MainWindow::MainWindow(QWidget*parent):

QMainWindow(parent),

ui(newUi::MainWindow)

ui-setupUi(this);

QAction*action_print=newQAction(tr("打印"),this);

QAction*action_printPreview=newQAction(tr("打印預(yù)覽"),this);

QAction*action_pdf=newQAction(tr("生成pdf"),this);

connect(action_print,SIGNAL(triggered()),this,SLOT(doPrint()));

connect(action_printPreview,SIGNAL(triggered()),this,SLOT(doPrintPreview()));

connect(action_pdf,SIGNAL(triggered()),this,SLOT(createPdf()));

ui-mainToolBar-addAction(action_print);

ui-mainToolBar-addAction(action_printPreview);

ui-mainToolBar-addAction(action_pdf);

MainWindow::~MainWindow()

deleteui;

voidMainWindow::doPrint()//打印

QPrinterprinter;//創(chuàng)建打印機對象

QPrintDialogdlg(printer,this);//創(chuàng)建打印對話框

//如果編輯器中有選中區(qū)域,則打印選中區(qū)域

if(ui-textEdit-textCursor().hasSelection())

dlg.addEnabledOption(QAbstractPrintDialog::PrintSelection);

if(dlg.exec()==QDialog::Accepted){//如果在對話框中按下了打印按鈕

ui-textEdit-print(printer);//則執(zhí)行打印操作

voidMainWindow::doPrintPreview()//打印預(yù)覽

QPrinterprinte

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論