Skip to content

Commit 05e3d68

Browse files
添加打印的实现
1 parent a8d6c8e commit 05e3d68

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

qt_print/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w;
8+
w.show();
9+
10+
return a.exec();
11+
}

qt_print/mainwindow.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
4+
#include <QPrinter>
5+
#include <QPrintDialog>
6+
#include <QPrintPreviewDialog>
7+
#include <QFileDialog>
8+
#include <QFileInfo>
9+
10+
11+
MainWindow::MainWindow(QWidget *parent) :
12+
QMainWindow(parent),
13+
ui(new Ui::MainWindow)
14+
{
15+
ui->setupUi(this);
16+
QAction *actionPrinter = new QAction(tr("打印"), this);
17+
QAction *actionPrintPreview = new QAction(tr("打印预览"), this);
18+
QAction *actionPdf = new QAction(tr("生成pdf"), this);
19+
20+
connect(actionPrinter, SIGNAL(triggered()), this, SLOT(doPrint));
21+
connect(actionPrintPreview, SIGNAL(triggered()), this, SLOT(doPrintPreview));
22+
connect(actionPdf, SIGNAL(triggered()), this, SLOT(createPdf));
23+
24+
25+
}
26+
27+
MainWindow::~MainWindow()
28+
{
29+
delete ui;
30+
}
31+
32+
void MainWindow::doPrint()
33+
{
34+
35+
}
36+
37+
void MainWindow::doPrintPreview()
38+
{
39+
40+
}
41+
42+
void MainWindow::printPreview(QPrinter *printer)
43+
{
44+
45+
}
46+
47+
void MainWindow::createPdf()
48+
{
49+
50+
}

qt_print/mainwindow.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
6+
namespace Ui {
7+
class MainWindow;
8+
}
9+
10+
class QPrinter;
11+
12+
class MainWindow : public QMainWindow
13+
{
14+
Q_OBJECT
15+
16+
public:
17+
explicit MainWindow(QWidget *parent = nullptr);
18+
~MainWindow();
19+
20+
21+
22+
private:
23+
void doPrint();
24+
void doPrintPreview();
25+
void printPreview(QPrinter *printer);
26+
void createPdf();
27+
28+
private:
29+
Ui::MainWindow *ui;
30+
};
31+
32+
#endif // MAINWINDOW_H

qt_print/mainwindow.ui

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>777</width>
10+
<height>366</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralWidget">
17+
<layout class="QGridLayout" name="gridLayout">
18+
<item row="0" column="0">
19+
<widget class="QTextEdit" name="textEdit"/>
20+
</item>
21+
</layout>
22+
</widget>
23+
<widget class="QMenuBar" name="menuBar">
24+
<property name="geometry">
25+
<rect>
26+
<x>0</x>
27+
<y>0</y>
28+
<width>777</width>
29+
<height>22</height>
30+
</rect>
31+
</property>
32+
</widget>
33+
<widget class="QToolBar" name="mainToolBar">
34+
<attribute name="toolBarArea">
35+
<enum>TopToolBarArea</enum>
36+
</attribute>
37+
<attribute name="toolBarBreak">
38+
<bool>false</bool>
39+
</attribute>
40+
</widget>
41+
<widget class="QStatusBar" name="statusBar"/>
42+
</widget>
43+
<layoutdefault spacing="6" margin="11"/>
44+
<resources/>
45+
<connections/>
46+
</ui>

qt_print/qt_print.pro

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2023-09-19T19:13:01
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui printsupport
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = qt_print
12+
TEMPLATE = app
13+
14+
# The following define makes your compiler emit warnings if you use
15+
# any feature of Qt which has been marked as deprecated (the exact warnings
16+
# depend on your compiler). Please consult the documentation of the
17+
# deprecated API in order to know how to port your code away from it.
18+
DEFINES += QT_DEPRECATED_WARNINGS
19+
20+
# You can also make your code fail to compile if you use deprecated APIs.
21+
# In order to do so, uncomment the following line.
22+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
23+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24+
25+
CONFIG += c++11
26+
27+
SOURCES += \
28+
main.cpp \
29+
mainwindow.cpp
30+
31+
HEADERS += \
32+
mainwindow.h
33+
34+
FORMS += \
35+
mainwindow.ui
36+
37+
# Default rules for deployment.
38+
qnx: target.path = /tmp/$${TARGET}/bin
39+
else: unix:!android: target.path = /opt/$${TARGET}/bin
40+
!isEmpty(target.path): INSTALLS += target

0 commit comments

Comments
 (0)