Qt设计:提取数字提取文字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/title71/article/details/79018244

##效果
这里写图片描述

##源码

##//mymainwindow.cpp

#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include <QString>
#include <qDebug>
#include <QPalette>
myMainWindow::myMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::myMainWindow)
{
    ui->setupUi(this);
    QPalette pe;
    QFont font("Arial",16);
}

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

void myMainWindow::on_pushButton_clicked()
{
    //获取数字
      QString str;
      QString res;
      QChar ch;
      str = ui->lineEdit->text();


      for(int i=0;i<str.size();i++)
      {
         ch = str.at(i);
          if(ch.toLatin1() <'0'||ch > '9')
              continue;
          if(((ch.toLatin1()-'0')%2)!=0||((ch.toLatin1()-'0')%2)!=1)
              res.append(ch);

          }

      QPalette pe;
      QFont font("Microsoft YaHei",16,75);

      pe.setColor(QPalette::Background,Qt::blue);
      pe.setColor(QPalette::WindowText,Qt::red);
      ui->label->setAutoFillBackground(true);
      ui->label->setPalette(pe);
      ui->label->setFont(font);
      ui->label->setText(res);
}

void myMainWindow::on_pushButton_2_clicked()
{
    //提取字符
       QString str;
       QString res;
       QChar ch;
       str = ui->lineEdit->text();

       for(int i=0;i<str.size();i++)
       {
          ch = str.at(i);
           if(ch.toLatin1() <'0'||ch > '9')
               res.append(ch);

           }

            QPalette pe;
            QFont font("Microsoft YaHei",16,75);
            pe.setColor(QPalette::WindowText,Qt::blue);
            pe.setColor(QPalette::Background,Qt::red);
            ui->label->setAutoFillBackground(true);
            ui->label->setPalette(pe);
            ui->label->setFont(font);
            ui->label->setText(res);
}

void myMainWindow::on_pushButton_3_clicked()
{
    close();
}

##//main.cpp

#include "mymainwindow.h"
#include <QApplication>

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

    return a.exec();
}

##//mymainwindow.h

#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class myMainWindow;
}

class myMainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();


    void on_pushButton_3_clicked();

private:
    Ui::myMainWindow *ui;
};

#endif // MYMAINWINDOW_H

猜你喜欢

转载自blog.csdn.net/title71/article/details/79018244