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
#include "mymainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myMainWindow w;
w.show();
return a.exec();
}
#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);
}
标签:奇偶,ch,提取,Qt,res,ui,pe,include,myMainWindow 来源: https://blog.csdn.net/title71/article/details/79008743