Qt设计:提取奇偶数

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

##效果图
这里写图片描述

这里写图片描述

这里写图片描述

##源码
##//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();

private:
    Ui::myMainWindow *ui;
};

#endif // MYMAINWINDOW_H

##//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.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);
}

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

void myMainWindow::on_pushButton_clicked()
{
    //获取奇数
      QString str;
      QString res;
      QChar ch;
      str = ui->lineEdit->text();
      if(!str.isEmpty())
          qDebug()<<str;

      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)
              res.append(ch);

          }
      QPalette pe;
      pe.setColor(QPalette::WindowText,Qt::red);
      ui->label->setPalette(pe);
      ui->label->setText(res);
}

void myMainWindow::on_pushButton_2_clicked()
{
    //提取偶数
        QString str;
       QString res;
       QChar ch;
       str = ui->lineEdit->text();
       if(!str.isEmpty())
           qDebug()<<str;

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

           }

            QPalette pe;
            pe.setColor(QPalette::WindowText,Qt::blue);
            ui->label_2->setPalette(pe);
            ui->label_2->setText(res);
}

猜你喜欢

转载自blog.csdn.net/title71/article/details/79008743
今日推荐