Qt设计:提取奇偶数

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

这里写图片描述

这里写图片描述

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

闲聊或者有任何建议都可以加QQ群交流:712573569

需回答问题:加群干什么吖

答案/回答:查水表

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wenlong Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值