qt1.4 simple realization WordPad

1 design goals:

qt1.4 simple realization WordPad
2 to create a project, add resources
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
3 Design of the Action
The QAction is a very useful class, interface design, the Action can be created, and completed the trigger () function of the signal slot.
Design Action features: You can create your menu options, toolbar buttons, set the associated Action QToolButton button when you click on these buttons will perform the function of Action of the slot.

1, add Action
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
2 design the menu bar
qt1.4 simple realization WordPad
3 can not add other components
such as: Do you need to add the toolbar Spinbox used to set the font size, FontcomboBox to select the font, add Lable in the status bar, ProgreeBar and retrieval system time and dynamic display in the window status bar, but refused to form components are added, the face of this situation, we need to write the appropriate code to accomplish it.

.h

#include <QLabel>
#include <QProgressBar>
#include <QSpinBox>
#include <QFontComboBox>
private slots:
    void timeUpdate();
private:
    Ui::MainWindow *ui;
    //工具栏添加组件
    QSpinBox *spinbox;
    QFontComboBox *Fontbox;
    //状态栏添加组件
    QLabel *label;
QProgressBar *progressBox;
QLabel  *currentTimeLabel;

.c

void MainWindow::Init()
 {
     //工具栏添加组件
//     QSpinBox *spinbox;
//     QFontComboBox *Fontbox;
     spinbox=new QSpinBox();
     spinbox->setMinimum(8);
     spinbox->setMaximum(50);
     spinbox->setValue(ui->textEdit->font().pointSize());
     ui->mainToolBar->addWidget(new QLabel("字体大小"));
     ui->mainToolBar->addWidget(spinbox);//spinbox添加到工具栏

     ui->mainToolBar->addSeparator();//添加分隔栏

     ui->mainToolBar->addWidget(new QLabel("字体"));
     Fontbox=new QFontComboBox;
     Fontbox->setMinimumWidth(150);
     ui->mainToolBar->addWidget(Fontbox);//Fontbox添加到工具栏

     setCentralWidget(ui->textEdit);

     //状态栏添加组件
//     QLabel *label;
//     QProgressBar *progressBox;
     label = new QLabel;
     label->setMidLineWidth(150);
     label->setText("当前文件:");
     ui->statusBar->addWidget(label);//label添加到状态栏

     progressBox = new QProgressBar;
     progressBox->setMinimum(5);
     progressBox->setMaximum(50);
     progressBox->setMinimumWidth(200);
     progressBox->setValue(ui->textEdit->font().pointSize());
     ui->statusBar->addWidget(progressBox);//progressBox添加到状态栏
//获取系统时间并动态显示在窗口状态栏
    currentTimeLabel=new QLabel;
     QTimer *timer=new QTimer(this);
     timer->start(1000); // 每次发射timeout信号时间间隔为1秒
     connect(timer,SIGNAL(timeout()),this,SLOT(timeUpdate()));

 }

Slot function:

void MainWindow::timeUpdate()
 {
    QDateTime CurrentTime=QDateTime::currentDateTime();
    QString Timestr=CurrentTime.toString(" yyyy年-MM月-dd日 hh:mm:ss "); //设置显示的格式
    currentTimeLabel->setText(Timestr);

   // ui->statusBar->addWidget(currentTimeLabel);//生成在状态栏的左端
    ui->statusBar->addPermanentWidget(currentTimeLabel);//生成在状态栏的最右端

 }
 }

To call the Init function in the constructor.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Init();

}

Operating results:
qt1.4 simple realization WordPad
4Action functions to achieve
editing functions to achieve Action:
qt1.4 simple realization WordPad
Other Action functions to achieve:
qt1.4 simple realization WordPad

void MainWindow::on_actBold_triggered(bool checked)
{
   QTextCharFormat fmt;
   fmt=ui->textEdit->currentCharFormat();
   if(checked)
   {
       fmt.setFontWeight(QFont::Bold);
   }
   else {
       fmt.setFontWeight(QFont::Normal);

   }
   ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_actItalic_triggered(bool checked)
{
    Q_UNUSED(checked);
    QTextCharFormat fmt;

    if (ui->actItalic->isChecked())
       fmt.setFontItalic(true);
    else
       fmt.setFontItalic(false);

    ui->textEdit->mergeCurrentCharFormat(fmt);
}

void MainWindow::on_actUnline_triggered(bool checked)
{
    Q_UNUSED(checked);
    QTextCharFormat fmt;

    if (ui->actUnline->isChecked())
       fmt.setFontUnderline(true);
    else
       fmt.setFontUnderline(false);

    ui->textEdit->mergeCurrentCharFormat(fmt);
}

Component signal created by the code implemented groove 5
first determine the write response code:
SpinBox signal:
void the valueChanged (I int)

FontComboBox signal:

void    valueChanged(int i) 

在void MainWindow::Init()中添加信号与槽的连接
//字体与大小信号与槽
connect(spinbox,SIGNAL(valueChanged(int)),this,SLOT(SpinBox_Valuechage(int)));
    connect(Fontbox,SIGNAL(currentIndexChanged(const QString &)),this,SLOT(ComboFont_currentIndexchange(const QString &)));
在.cpp中添加槽函数的实现

void MainWindow::SpinBox_Valuechage(int SBoxFontSize)
{
    qDebug()<<"11";
   QTextCharFormat format;
   format.setFontPointSize(SBoxFontSize);
   ui->textEdit->mergeCurrentCharFormat(format);
   progressBox->setValue(SBoxFontSize);
}
//改变字体格式
void MainWindow::ComboFont_currentIndexchange(const QString &arg1)
{
    QTextCharFormat format;
    format.setFontFamily(arg1);
    ui->textEdit->mergeCurrentCharFormat(format);
}

Operating results:
qt1.4 simple realization WordPad
6 add the following line to achieve in the Init function
qt1.4 simple realization WordPadui-> mainToolBar-> setToolButtonStyle (Qt :: ToolButtonTextUnderIcon); // display text and icons
7 Add an icon for the application
to copy the icon file to the project file directory, and .pro file, add RC_ICONS = name.ico.
name is the image file name, including the extension must be .ico picture
qt1.4 simple realization WordPad
qt1.4 simple realization WordPad
8 file operations would say later.

Guess you like

Origin blog.51cto.com/14165014/2452866