码迷,mamicode.com
首页 > 其他好文 > 详细

2000行之Qt简化写字板

时间:2017-05-29 18:22:17      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:blog   font   show   war   sep   format   cpp   dialog   UI   

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
namespace Ui {
class MainWindow;
}

class QLineEdit;
class QDialog;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QLineEdit *lineedit;
    QDialog *finddialog;
    QLabel *permanent2;

private slots:
    void textFind();//查找文本
    void findNext();//查找下一个
    void addunderline();//加下划线
    void songtype();
    void kaitype();
    void blacktype();
};

#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QTextFrame>
#include <QString>
#include <QLineEdit>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //菜单栏
    ui->setupUi(this);
    QIcon icona(":/images/f.png");//创建图标
    QIcon iconb(":/images/x.png");
    QIcon iconc(":/images/z.png");
    QMenu *funMenu = ui->menuBar->addMenu(tr("功能(&G)"));//添加菜单
    QAction *action_find = funMenu->addAction(tr("查找(&F)"));//运行程序时,按下Alt+F就可以进行查找
    QAction *action_underline = funMenu->addAction(tr("下划线(&X)"));
    QMenu *fontMenu = ui->menuBar->addMenu(tr("字体(&Z)"));
    QActionGroup *fontgroup = new QActionGroup(this);//建立动作组
    QAction *action_A = fontgroup->addAction(tr("宋体(&S)"));//向动作组中添加动作
    QAction *action_B = fontgroup->addAction(tr("黑体(&H)"));
    QAction *action_C = fontgroup->addAction(tr("楷体(&K)"));
    action_A->setCheckable(true);//选中菜单时,就会用线框将图标围住
    action_B->setCheckable(true);
    action_C->setCheckable(true);
    action_A->setIcon(icona);//添加图标
    action_B->setIcon(iconb);
    action_C->setIcon(iconc);
    action_A->setChecked(true);//指定action_A为选中状态
    fontMenu->addSeparator();//向菜单中添加间隔器
    fontMenu->addAction(action_A);//向菜单中添加状态
    fontMenu->addAction(action_B);
    fontMenu->addAction(action_C);

    //状态栏
    QLabel *permanent1 = new QLabel(this);
    //QLabel *permanent3 = new QLabel(this);
    //QTextCursor cursor = ui->textEdit->textCursor();
    permanent1->setText(tr("华中农业大学信息学院"));
    //permanent3->setText(tr("行数:%1列数:%2").arg(cursor.blockNumber()).arg(cursor.columnNumber()));
    ui->statusBar->addPermanentWidget(permanent1);


    //查找
    connect(action_find, &QAction::triggered, this, &MainWindow::textFind);//连接信号和槽
    finddialog = new QDialog(this);
    lineedit = new QLineEdit(finddialog);
    permanent2 = new QLabel(finddialog);
    QPushButton *btn = new QPushButton(finddialog);
    btn->setText(tr("查找下一个"));
    connect(btn, &QPushButton::clicked, this, &MainWindow::findNext);
    QVBoxLayout *layout = new QVBoxLayout;//使用垂直布局
    layout->addWidget(lineedit);
    layout->addWidget(permanent2);
    layout->addWidget(btn);
    finddialog->setLayout(layout);

    //加下划线
    connect(action_underline, &QAction::triggered, this, &MainWindow::addunderline);
    //修改字体
    connect(action_A, &QAction::triggered, this, &MainWindow::songtype);
    connect(action_B, &QAction::triggered, this, &MainWindow::kaitype);
    connect(action_C, &QAction::triggered, this, &MainWindow::blacktype);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::textFind()
{
    finddialog->resize(200,200);//设置对话框大小
    finddialog->show();
}

void MainWindow::findNext()
{
    QString string = lineedit->text();
    bool isfind = ui->textEdit->find(string, QTextDocument::FindBackward);//向后查找
    if(isfind){
        permanent2->setText(tr("行号:%1列号:%2").arg(ui->textEdit->textCursor().blockNumber())//显示查找到字符的坐标
                            .arg(ui->textEdit->textCursor().columnNumber()));

    }
}

void MainWindow::addunderline()
{
    QTextCharFormat charformat;
    charformat.setFontUnderline(true);
    ui->textEdit->setCurrentCharFormat(charformat);//之后输入的字带有下划线
}

void MainWindow::songtype()
{
    QTextCharFormat charformat;
    charformat.setFont(tr("宋体"));
    ui->textEdit->setCurrentCharFormat(charformat);
}

void MainWindow::kaitype()
{
    QTextCharFormat charformat;
    charformat.setFont(tr("黑体"));
    ui->textEdit->setCurrentCharFormat(charformat);
}

void MainWindow::blacktype()
{
    QTextCharFormat charformat;
    charformat.setFont(tr("楷体"));
    ui->textEdit->setCurrentCharFormat(charformat);
}
//main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

2000行之Qt简化写字板

标签:blog   font   show   war   sep   format   cpp   dialog   UI   

原文地址:http://www.cnblogs.com/qrxqrx/p/6918525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!