qt编程问题,请高手指教 5
#include "mainwindow.h"
#include <QGridLayout>
const static double PI=3.1416;
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
label1=new QLabel(this);
label1->setText(tr("<i>请输入圆的半径:</i>"));
lineEdit=new QLineEdit(this);
label2=new QLabel(this);
button=new QPushButton(this);
button->setText(tr("<font color=red>显示对应圆的面积</font>"));
QGridLayout *mainLayout=new QGridLayout(this);
mainLayout->addWidget(label1,0,0);
mainLayout->addWidget(lineEdit,0,1);
mainLayout->addWidget(label2,1,0);
mainLayout->addWidget(button,1,1);
// setLayout(mainLayout);
connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea()));
}
void Dialog::showArea()
{
bool ok;
QString tempStr;
QString valueStr=lineEdit->text();
int valueInt=valueStr.toInt(&ok);
double area=valueInt*valueInt*PI;
label2->setText(tempStr.setNum(area));
}
Dialog::~Dialog()
{
}
//main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
Dialog w;
w.show();
return a.exec();
}
//mainwindow.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void showArea();
private:
QLabel *label1,*label2;
QLineEdit *lineEdit;
QPushButton *button;
};
#endif // DIALOG_H
显示对应圆面积出错了 没有变成红色啊???
请高手指教,本人刚接触qt 展开
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
label1=new QLabel(this);
label1->setText(tr("<i>请输入圆的半径:</i>"));
lineEdit=new QLineEdit(this);
label2=new QLabel(this);
QPalette palette;
palette.setColor(QPalette::WindowText,Qt::red);
label2->setPalette(palette);
button=new QPushButton(this);
palette.setColor(QPalette::ButtonText,Qt::red);
button->setPalette(palette);
button->setText(tr("<font color=red>显示对应圆的面积</font>"));
QGridLayout *mainLayout=new QGridLayout(this);
mainLayout->addWidget(label1,0,0);
mainLayout->addWidget(lineEdit,0,1);
mainLayout->addWidget(label2,1,0);
mainLayout->addWidget(button,1,1);
// setLayout(mainLayout);
connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea()));
}