±à¼ÍƼö: |
±¾ÎÄÀ´×ÔÓÚcsdn£¬±¾ÎÄÖ÷Òª½éÉÜÁËÒ»¸öQt5µÄÎı¾±à¼Æ÷µÄʵÀýµÄÏêϸԴ´úÂ룬ϣÍû¶Ô´ó¼ÒµÄѧϰÄÜÓаïÖú¡£ |
|
Ч¹ûͼ£º
³ÌÐòÊÇÒ»¸öÎı¾±à¼Æ÷µÄʵÀý£¬Ö÷½çÃæÈçÏ£º

³ÌÐòÖеÄÔ´ÎļþºÍÍ·Îļþ£º

Ô´´úÂ룺
Îļþmain.cppµÄÔ´´úÂ룺
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont font("¿¬Ìå",15);
a.setFont(font);
MainWindow w;
w.show();
return a.exec();
} |
mainwindow.hÔ´´úÂ룺
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBox>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>
#include <QActionGroup>
#include <QAction>
#include "showwidget.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void createActions(); //´´½¨¶¯×÷
void createMenus(); //´´½¨²Ëµ¥
void createToolBars(); //´´½¨¹¤¾ßÀ¸
void loadFile(QString filename);
void mergeFormat(QTextCharFormat format);
private:
QMenu *fileMenu; //¸÷Ïî²Ëµ¥À¸
QMenu *zoomMenu;
QMenu *rotateMenu;
QMenu *mirrorMenu;
QImage img;
QString fileName;
ShowWidget *showWidget;
QAction *openFileAction; //Îļþ²Ëµ¥Ïî
QAction *newFileAction;
QAction *printTextAction;
QAction *printImageAction;
QAction *exitAction;
QAction *copyAction; //±à¼²Ëµ¥Ïî
QAction *cutAction;
QAction *pasteAction;
QAction *aboutAction;
QAction *zoomInAction;
QAction *zoomOutAction;
QAction *rotate90Action; //Ðýת²Ëµ¥Ïî
QAction *rotate180Action;
QAction *rotate270Action;
QAction *mirrorVerticalAction; //¾µÏñ²Ëµ¥Ïî
QAction *mirrorHorizontalAction;
QAction *undoAction; //³·ÏúºÍ·µ»Ø
QAction *redoAction;
QToolBar *fileTool; //¹¤¾ßÀ¸
QToolBar *zoomTool;
QToolBar *rotateTool;
QToolBar *mirrorTool;
QToolBar *doToolBar; //¹¤¾ßÀ¸
QLabel *fontLabel1; //×ÖÌåÉèÖÃÏî
QFontComboBox *fontComboBox;
QLabel *fontLabel2;
QComboBox *sizeComboBox;
QToolButton *boldBtn;
QToolButton *italicBtn;
QToolButton *underlineBtn;
QToolButton *colorBtn;
QToolBar *fontToolBar; //×ÖÌ幤¾ßÀ¸
QLabel *listLabel; //ÎÄ×ÖÅŰ湦ÄÜ
QComboBox *listComboBox;
QActionGroup *actGrp;
QAction *leftAction;
QAction *rightAction;
QAction *centerAction;
QAction *justifyAction;
QToolBar *listToolBar;
protected slots:
void showNewFile(); //н¨Îļþ²Ûº¯Êý
void showOpenFile(); //´ò¿ªÎļþ²Ûº¯Êý
void showPrintText(); //´òÓ¡Îı¾²Ûº¯Êý
void showPrintImage(); //´òӡͼƬ²Ûº¯Êý
void showZoomIn(); //·Å´ó¹¦Äܲۺ¯Êý
void showZoomOut(); //ËõС¹¦Äܲۺ¯Êý
void showRotate90(); //ÐýתͼƬ²Ûº¯Êý
void showRotate180();
void showRotate270();
void showMirrorVertical(); //¾µÏñ¹¦ÄÜ
void showMirrorHorizontal();
void showFontComboBox(QString comboStr); //×ÖÌåÉèÖÃÏà¹ØµÄ²Ûº¯Êý
void showSizeSpinBix(QString spinValue);
void showBoldBtn();
void showItalicBtn();
void showUnderlineBtn();
void showColorBtn();
void showCurrentFormatChanged(const QTextCharFormat
&fmt);
void showList(int);
void showAlignment(QAction *act);
void showCursorPositionChanged();
};
#endif // MAINWINDOW_H |
mainwindow.cppÔ´´úÂ룺
#include "mainwindow.h"
#include "showwidget.h"
#include <QApplication>
#include <QToolBar>
#include <QFileDialog>
#include <QTextStream>
#include <QtPrintSupport/QPrinter>
#include <QtPrintSupport/QPrintDialog>
#include <QPainter>
#include <QRect>
#include <QSize>
#include <QGraphicsItem>
#include <QColorDialog>
#include <QColor>
#include <QAction>
#include <QTextList>
#include <QTextBlockFormat>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("Easy Word"));
showWidget = new ShowWidget(this);
setCentralWidget(showWidget);
fontLabel1 = new QLabel(tr("×ÖÌå: "));
fontComboBox = new QFontComboBox;
fontComboBox->setFontFilters (QFontComboBox::ScalableFonts);
fontLabel2 = new QLabel(tr("×ÖºÅ: "));
sizeComboBox = new QComboBox;
QFontDatabase db;
foreach (int size ,db.standardSizes())
{
sizeComboBox->addItem(QString::number(size));
}
boldBtn = new QToolButton;
boldBtn->setIcon(QIcon("bold.png"));
boldBtn->setCheckable(true);
italicBtn = new QToolButton;
italicBtn->setIcon(QIcon("italic.png"));
italicBtn->setCheckable(true);
underlineBtn = new QToolButton;
underlineBtn->setIcon(QIcon("underline.png"));
underlineBtn->setCheckable(true);
colorBtn = new QToolButton;
colorBtn->setIcon(QIcon("color.png"));
colorBtn->setCheckable(true);
//ÎÄ×ÖÅŰ湦ÄÜ
listLabel = new QLabel(tr("ÅÅÐò"));
listComboBox = new QComboBox;
listComboBox->addItem("Standard");
listComboBox->addItem("QTextListFormat::ListDisc");
listComboBox->addItem("QTextListFormat::ListCircle");
listComboBox->addItem("QTextListFormat::ListSquare");
listComboBox->addItem("QTextListFormat::ListDecimal");
listComboBox->addItem("QTextListFormat::ListLowerAlpha");
listComboBox->addItem("QTextListFormat::ListUpperAlpha");
listComboBox->addItem("QTextListFormat::ListLowerRoman");
listComboBox->addItem("QTextListFormat::ListUpperRoman");
createActions();
createMenus();
createToolBars();
if(img.load("image.png"))
{
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
//¸÷×ÖÌåÉèÖÃÏà¹Ø¹¦ÄܵÄÐźÅÓë²Û
connect(fontComboBox,SIGNAL(activated(QString)), this,SLOT(showFontComboBox(QString)));
connect(sizeComboBox,SIGNAL(activated(QString)), this,SLOT(showSizeSpinBix(QString)));
connect(boldBtn,SIGNAL(clicked(bool)), this,SLOT(showBoldBtn()));
connect(italicBtn,SIGNAL(clicked(bool)), this,SLOT(showItalicBtn()));
connect(underlineBtn,SIGNAL(clicked(bool)), this,SLOT(showUnderlineBtn()));
connect(colorBtn,SIGNAL(clicked(bool)), this,SLOT(showColorBtn()));
connect(showWidget->text,SIGNAL (currentCharFormatChanged (QTextCharFormat
&)),this, SLOT(showCurrentFormatChanged(QTextCharFormat
&)));
// //ÎÄ×ÖÅŰ湦ÄܵÄÐźÅÓë²Û
// connect(listComboBox,SIGNAL(activated(int)), this,SLOT(showList(int)));
// connect(showWidget->text->document() ,SIGNAL(undoAvailable(bool)) ,redoAction,SLOT(setEnabled(bool)));
// connect(showWidget->text->document() ,SIGNAL(redoAvailable(bool)) ,redoAction,SLOT(setEnabled(bool)));
// connect(showWidget->text, SIGNAL(cursorPositionChanged()), this,SLOT(showCursorPositionChanged()));
connect(listComboBox, SIGNAL(activated(int)), this,SLOT(showList(int)));
connect(showWidget->text->document(), SIGNAL(undoAvailable(bool)) ,redoAction,SLOT(setEnabled(bool)));
connect(showWidget->text->document(), SIGNAL(redoAvailable(bool)) ,redoAction,SLOT(setEnabled(bool)));
connect(showWidget->text, SIGNAL(cursorPositionChanged()) ,this,SLOT(showCursorPositionChanged()));
}
void MainWindow::createActions()
{
//¡°´ò¿ª¡±¶¯×÷
openFileAction = new QAction(QIcon("open.png")
, tr("´ò¿ª") , this);
openFileAction->setShortcut(tr("Ctrl +
O"));
openFileAction->setStatusTip(tr("´ò¿ªÒ»¸öÎļþ"));
connect(openFileAction,SIGNAL(triggered(bool)) ,this,SLOT(showOpenFile()));
//¡°Ð½¨¡±¶¯×÷
newFileAction = new QAction(QIcon("new.png")
, tr("н¨") ,this );
newFileAction->setShortcut(tr("Ctrl +
N"));
newFileAction->setStatusTip(tr("н¨Ò»¸öÎļþ"));
connect(newFileAction,SIGNAL(triggered()), this,SLOT(showNewFile()));
//"Í˳ö"¶¯×÷
exitAction = new QAction(tr("Í˳ö"), this);
exitAction->setShortcut(tr("Ctrl + Q"));
exitAction->setStatusTip(tr("Í˳ö³ÌÐò"));
connect(exitAction,SIGNAL(triggered(bool)) ,this,SLOT(close()));
//"¸´ÖÆ"¶¯×÷
copyAction = new QAction(QIcon("copy.png")
, tr("¸´ÖÆ") , this);
copyAction->setShortcut(tr("Ctrl + C"));
copyAction->setStatusTip(tr("¸´ÖÆÎļþ"));
connect(copyAction,SIGNAL(triggered(bool)), showWidget->text,SLOT(copy()));
//¼ôÇвÙ×÷
cutAction = new QAction(QIcon("cut.png")
, tr("¼ôÇÐ") ,this);
cutAction->setShortcut(tr("Ctrl + X"));
cutAction->setStatusTip(tr("¼ôÇÐÎļþ"));
connect(cutAction,SIGNAL(triggered(bool)), showWidget->text,SLOT(cut()));
//Õ³Ìù²Ù×÷
pasteAction = new QAction(QIcon("paste.png")
, tr("Õ³Ìù") ,this);
pasteAction->setShortcut(tr("Ctrl + V"));
pasteAction->setStatusTip(tr("Õ³ÌùÎļþ"));
connect(pasteAction,SIGNAL(triggered(bool)), showWidget->text,SLOT(paste()));
//¡°¹ØÓÚ¡±²Ù×÷
aboutAction = new QAction(tr("¹ØÓÚ") ,this);
connect(aboutAction, SIGNAL(triggered(bool)) ,this,
SLOT(Qapplication::aboutQt()));
//´òÓ¡Îı¾
printTextAction = new QAction(QIcon("printText.png")
, tr("´òÓ¡Îı¾") ,this);
printTextAction->setStatusTip(tr("´òÓ¡Ò»¸öÎı¾"));
connect(printTextAction,SIGNAL(triggered(bool)) ,this,SLOT(showPrintText()));
//´òӡͼƬ
printImageAction = new QAction(QIcon("printImage.png")
, tr("´òӡͼÏñ") , this);
printImageAction->setStatusTip(tr("´òÓ¡Ò»·ùͼÏñ"));
connect(printImageAction,SIGNAL(triggered(bool)), this,SLOT(showPrintImage()));
//·Å´óͼƬ
zoomInAction = new QAction(QIcon("zoomin.png")
, tr("·Å´ó") ,this);
zoomInAction->setStatusTip(tr("·Å´óÒ»ÕÅͼƬ"));
connect(zoomInAction,SIGNAL(triggered(bool)), this,SLOT(showZoomIn()));
//ËõСͼƬ
zoomOutAction = new QAction(QIcon("zoomout.png")
,tr("ËõС") ,this);
zoomOutAction->setStatusTip(tr("ËõСһÕÅͼƬ"));
connect(zoomOutAction,SIGNAL(triggered(bool)) ,this,SLOT(showZoomOut()));
//ÐýתͼƬ
rotate90Action = new QAction(QIcon("rotate90.png")
, tr("Ðýת90") ,this);
rotate90Action->setStatusTip(tr("½«Ò»·ùͼÐýת90¶È"));
connect(rotate90Action,SIGNAL(triggered(bool)) ,this,SLOT(showRotate90()));
rotate180Action = new QAction(QIcon("rotate180.png")
,tr("Ðýת180") ,this);
rotate180Action->setStatusTip(tr("½«Ò»·ùͼƬÐýת180¶È"));
connect(rotate180Action,SIGNAL(triggered(bool)), this,SLOT(showRotate180()));
rotate270Action = new QAction(QIcon("rotate270.png"),
tr("Ðýת270") ,this);
rotate270Action->setStatusTip(tr("½«Ò»·ùͼƬÐýת270¶È"));
connect(rotate270Action,SIGNAL(triggered(bool)), this,SLOT(showRotate270()));
//´¹Ö±¾µÏñ
mirrorVerticalAction = new QAction(QIcon("mirrorVertical.png")
, tr("×ÝÏò¾µÏñ") ,this);
mirrorVerticalAction->setStatusTip(tr("¶ÔÒ»ÕÅͼƬ×ö×ÝÏò¾µÏñ"));
connect(mirrorVerticalAction,SIGNAL(triggered(bool)), this,SLOT(showMirrorVertical()));
//ˮƽ¾µÏñ
mirrorHorizontalAction = new QAction (QIcon("mirrorHorizontal.png")
,tr("ºáÏò¾µÏñ") ,this);
mirrorHorizontalAction->setStatusTip(tr("¶ÔÒ»ÕÅͼƬ×öºáÏò¾µÏñ"));
connect(mirrorHorizontalAction,SIGNAL(triggered(bool)) ,this,SLOT(showMirrorHorizontal()));
//³·Ïú²Ù×÷
undoAction = new QAction(QIcon("undo.png"),
tr("³·Ïú") ,this);
connect(undoAction, SIGNAL(triggered(bool)) ,showWidget->text,SLOT(undo()));
//ÖØ×ö²Ù×÷
redoAction = new QAction(QIcon("redo.png")
,tr("ÖØ×ö") ,this);
connect(redoAction,SIGNAL(triggered(bool)) ,showWidget->text,SLOT(redo()));
//ÎÄ×ÖÅŰ湦ÄÜ
actGrp = new QActionGroup(this);
leftAction = new QAction(QIcon("left.png") ,"×ó¶ÔÆë"
,actGrp);
leftAction->setCheckable(true);
rightAction = new QAction(QIcon("right.png")
, "ÓÒ¶ÔÆë" ,actGrp);
rightAction->setCheckable(true);
centerAction = new QAction(QIcon("center.png")
, "¾ÓÖÐ" ,actGrp);
centerAction->setCheckable(true);
justifyAction = new QAction(QIcon("justify.png")
, "Á½¶Ë¶ÔÆë" , actGrp);
justifyAction->setCheckable(true);
connect(actGrp,SIGNAL(triggered(QAction*)) ,this,SLOT(ShowAlignment(QAction*)));
}
void MainWindow::createMenus() //´´½¨²Ëµ¥À¸
{
fileMenu = menuBar()->addMenu(tr("Îļþ"));
fileMenu->addAction(openFileAction);
fileMenu->addAction(newFileAction);
fileMenu->addAction(printTextAction);
fileMenu->addAction(printImageAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
zoomMenu = menuBar()->addMenu(tr("±à¼"));
zoomMenu->addAction(copyAction);
zoomMenu->addAction(cutAction);
zoomMenu->addAction(pasteAction);
zoomMenu->addAction(aboutAction);
zoomMenu->addSeparator();
zoomMenu->addAction(zoomInAction);
zoomMenu->addAction(zoomOutAction);
rotateMenu = menuBar()->addMenu(tr("Ðýת"));
rotateMenu->addAction(rotate90Action);
rotateMenu->addAction(rotate180Action);
rotateMenu->addAction(rotate270Action);
mirrorMenu = menuBar()->addMenu(tr("¾µÏñ"));
mirrorMenu->addAction(mirrorVerticalAction);
mirrorMenu->addAction(mirrorHorizontalAction);
}
void MainWindow::createToolBars() //´´½¨¹¤¾ßÀ¸
{
fileTool = addToolBar("File");
fileTool->addAction(openFileAction);
fileTool->addAction(newFileAction);
fileTool->addAction(printTextAction);
fileTool->addAction(printImageAction);
zoomTool = addToolBar("Edit"); //´´½¨ÎÄ×ֱ༹¤¾ßÀ¸
zoomTool->addAction(copyAction);
zoomTool->addAction(cutAction);
zoomTool->addAction(pasteAction);
zoomTool->addSeparator();
zoomTool->addAction(zoomInAction);
zoomTool->addAction(zoomOutAction);
rotateTool = addToolBar("Rotate"); //´´½¨Ðýת¹¤¾ßÀ¸
rotateTool->addAction(rotate90Action);
rotateTool->addAction(rotate180Action);
rotateTool->addAction(rotate270Action);
doToolBar = addToolBar("DoEdit"); //´´½¨³·ÏúºÍÖØ×ö¹¤¾ßÀ¸
doToolBar->addAction(undoAction);
doToolBar->addAction(redoAction);
fontToolBar = addToolBar("Font"); //´´½¨×ÖÌ幤¾ßÀ¸
fontToolBar->addWidget(fontLabel1);
fontToolBar->addWidget(fontComboBox);
fontToolBar->addWidget(fontLabel2);
fontToolBar->addWidget(sizeComboBox);
fontToolBar->addSeparator(); //¼ÓÈë·Ö¸îÏß
fontToolBar->addWidget(boldBtn);
fontToolBar->addWidget(italicBtn);
fontToolBar->addWidget(underlineBtn);
fontToolBar->addSeparator();
fontToolBar->addWidget(colorBtn);
listToolBar = addToolBar("list"); //´´½¨ÎÄ×ÖÅŰ湤¾ßÀ¸
listToolBar->addWidget(listLabel);
listToolBar->addWidget(listComboBox);
listToolBar->addSeparator();
listToolBar->addActions(actGrp->actions());
}
void MainWindow::showNewFile() //н¨Îļþ²Ù×÷
{
MainWindow *newMainWindow = new MainWindow;
newMainWindow->show();
}
void MainWindow::showOpenFile() //¶ÁÎı¾ÊÇ´ò¿ªÐ´°¿Ú
{
fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
{
if(showWidget->text->document()->isEmpty())
{
loadFile(fileName);
}
else
{
MainWindow *newMainWindow = new MainWindow;
newMainWindow->show(); //µ±Ò»¸ö´°¿ÚÒѾ´ò¿ªÁËÒ»¸öÎļþ£¬»¹ÐèÒªÔÙ´ò¿ªÎļþʱ£¬ÐÂÉú³É´°¿Ú
newMainWindow->loadFile(fileName);
}
}
}
void MainWindow::loadFile(QString fileName) //ÔØÈëÎļþ
{
printf("File name:%s\n",fileName.data());
QFile file(fileName);
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream textStream(&file);
while(!textStream.atEnd())
{
showWidget->text->append(textStream.readLine());
printf("Read line\n");
}
printf("End\\n");
}
}
void MainWindow::showPrintText() //´òÓ¡Îı¾
{
QPrinter printer;
QPrintDialog printDialog;
if(printDialog.exec())
{
QTextDocument *doc = showWidget->text->document();
doc->print(&printer);
}
}
void MainWindow::showPrintImage() //´òӡͼƬ
{
QPrinter printer;
QPrintDialog printDialog;
if(printDialog.exec())
{
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = img.size();
size.scale(rect.size() , Qt::KeepAspectRatio);
painter.setViewport(rect.x() , rect.y(), size.width(),
size.height());
painter.setWindow(img.rect());
painter.drawImage(0,0,img);
}
}
void MainWindow::showZoomIn() //ͼƬ·Å´ó
{
if(img.isNull())
{
return ;
}
QMatrix martix;
martix.scale(2,2);
img = img.transformed(martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage(img));
}
void MainWindow::showZoomOut() //ͼƬËõС
{
if(img.isNull())
{
return ;
}
QMatrix martix;
martix.scale(0.5,0.5);
img = img.transformed(martix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage(img));
}
void MainWindow::showRotate90() //ÐýתͼƬ¹¦ÄÜ
{
if(img.isNull())
{
return;
}
QMatrix matrix;
matrix.rotate(90);
img = img.transformed(matrix);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::showRotate180()
{
if(img.isNull())
{
return;
}
QMatrix matrix;
matrix.rotate(180);
img = img.transformed(matrix);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::showRotate270()
{
if(img.isNull())
{
return;
}
QMatrix matrix;
matrix.rotate(270);
img = img.transformed(matrix);
showWidget->imageLabel->setPixmap (QPixmap::fromImage(img));
}
void MainWindow::showMirrorHorizontal() //ˮƽ¾µÏñ
{
if(img.isNull())
{
return ;
}
img = img.mirrored(true,false);
showWidget->imageLabel->setPixmap (QPixmap::fromImage(img));
}
void MainWindow::showMirrorVertical() //´¹Ö±¾µÏñ
{
if(img.isNull())
{
return ;
}
img = img.mirrored(false, true);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}
void MainWindow::showFontComboBox(QString comboStr)
//ÉèÖÃ×ÖÌå
{
QTextCharFormat fmt;
fmt.setFontFamily(comboStr);
mergeFormat(fmt);
}
void MainWindow::mergeFormat(QTextCharFormat format)
{
QTextCursor cursor = showWidget->text->textCursor();
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::WordUnderCursor);
}
cursor.mergeCharFormat(format);
showWidget->text->mergeCurrentCharFormat(format);
}
void MainWindow::showSizeSpinBix(QString spinvalue)
//ÉèÖÃ×ÖºÅ
{
QTextCharFormat fmt;
fmt.setFontPointSize(spinvalue.toFloat());
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::showBoldBtn() //ÉèÖÃÎÄ×Ö¼Ó´Ö
{
QTextCharFormat fmt;
fmt.setFontWeight(boldBtn->isCheckable() ?
QFont::Bold : QFont::Normal);
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::showItalicBtn()
{
QTextCharFormat fmt;
fmt.setFontItalic(italicBtn->isCheckable());
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::showUnderlineBtn()
{
QTextCharFormat fmt;
fmt.setFontUnderline(underlineBtn->isCheckable());
showWidget->text->mergeCurrentCharFormat(fmt);
}
void MainWindow::showColorBtn()
{
QColor color = QColorDialog::getColor(Qt::red,
this);
if(color.isValid())
{
QTextCharFormat fmt;
fmt.setForeground(color);
showWidget->text->mergeCurrentCharFormat(fmt);
}
}
void MainWindow::showCurrentFormatChanged(const
QTextCharFormat &fmt)
{
fontComboBox->setCurrentIndex(fontComboBox-> findText(fmt.fontFamily()));
sizeComboBox->setCurrentIndex(sizeComboBox-> findText(QString::number(fmt.fontPointSize())));
boldBtn->setCheckable(fmt.font().bold());
italicBtn->setCheckable(fmt.fontItalic());
underlineBtn->setCheckable(fmt.fontUnderline());
}
void MainWindow::showAlignment(QAction *act)
{
if(act == leftAction)
{
showWidget->text->setAlignment(Qt::AlignLeft);
}
if(act == rightAction)
{
showWidget->text->setAlignment(Qt::AlignRight);
}
if(act == centerAction)
{
showWidget->text->setAlignment(Qt::AlignCenter);
}
if(act == justifyAction)
{
showWidget->text->setAlignment(Qt::AlignJustify);
}
}
void MainWindow::showCursorPositionChanged()
{
if(showWidget->text->alignment() == Qt::AlignLeft)
{
leftAction->setCheckable(true);
}
if(showWidget->text->alignment() == Qt::AlignRight)
{
rightAction->setCheckable(true);
}
if(showWidget->text->alignment() == Qt::AlignCenter)
{
centerAction->setCheckable(true);
}
if(showWidget->text->alignment() == Qt::AlignJustify)
{
justifyAction->setCheckable(true);
}
}
void MainWindow::showList(int index)
{
QTextCursor cursor = showWidget->text->textCursor();
if(index != 0)
{
QTextListFormat::Style style = QTextListFormat::ListDisc;
switch(index)
{
default:
case 1 :
style = QTextListFormat::ListDisc;
break;
case 2:
style = QTextListFormat::ListCircle;
break;
case 3:
style = QTextListFormat::ListSquare;
break;
case 4:
style = QTextListFormat::ListDecimal;
break;
case 5:
style = QTextListFormat::ListLowerAlpha;
break;
case 6:
style = QTextListFormat::ListUpperAlpha;
break;
case 7:
style = QTextListFormat::ListLowerRoman;
break;
case 8:
style = QTextListFormat::ListUpperRoman;
}
cursor.beginEditBlock(); //ÉèÖÃËõ½øÖµ
QTextBlockFormat blockFmt = cursor.blockFormat();
QTextListFormat listFmt;
if(cursor.currentList())
{
listFmt = cursor.currentList()->format();
}
else
{
listFmt.setIndent(blockFmt.indent() + 1);
blockFmt.setIndent(0);
cursor.setBlockFormat(blockFmt);
}
listFmt.setStyle(style);
cursor.createList(listFmt);
cursor.endEditBlock();
}
else
{
QTextBlockFormat bfmt;
bfmt.setObjectIndex(-1);
cursor.mergeBlockFormat(bfmt);
}
}
MainWindow::~MainWindow()
{
} |
showwidget.hÔ´´úÂ룺
#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QTextEdit>
#include <QImage>
class ShowWidget : public QWidget
{
Q_OBJECT
public:
explicit ShowWidget(QWidget *parent = 0);
QImage img;
QLabel *imageLabel;
QTextEdit *text;
signals:
public slots:
};
#endif // SHOWWIDGET_H |
showwidget.cppÔ´´úÂ룺
#include "showwidget.h"
#include <QHBoxLayout>
ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent)
{
imageLabel = new QLabel;
imageLabel->setScaledContents(true); //ÉèÖÃÔÊÐí¿Ø¼þ×Ô¶¯µ÷Õû´óС,ÒÔʹÄÚÈÝÌî³äÕû¸öÓÐÐ§ÇøÓò
text = new QTextEdit;
QHBoxLayout *mainLayout = new QHBoxLayout(this);
//ˮƽºÐ×Ó²¼¾Ö
mainLayout->addWidget(imageLabel);
mainLayout->addWidget(text);
} |
³ÌÐòÖÐδ½â¾öµÄbug:
1¡¢Îı¾¶ÎÂä¶ÔÆë¹¦ÄÜÓÐÎÊÌ⣬²»ÄÜʵÏ֯书ÄÜ¡££¨ÎÒ×Ðϸ¼ì²éÁË´úÂ룬ÈÔδ·¢ÏÖÎÊÌâËùÔÚ£¬Â·¹ýµÄ´óÉñ¸÷ÏÔÉñͨ°É£¬Íû²»Ï§³à½Å£©¡£
Ô´´úÂëÏÂÔØµØÖ·£ºhttp://download.csdn.net/detail/rl529014/9546463 |