当前位置: 技术问答>linux和unix
新手求助QT编程,如何动态设置QLineEdit控件的内容?
来源: 互联网 发布时间:2015-11-06
本文导语: 举一个简单的例子 一个QDialog上有三个QLineEdit控件,分别为le_1,le_2,le_3 和一个QPushButton控件btn_1; 现在想通过单击btn_1这个按钮,来取出le_1和le_2中的内容,设置到le_3里去? connect这个怎么写? connect(btn_1,SIGNAL(clicked),le_3,SLOT(?))...
举一个简单的例子
一个QDialog上有三个QLineEdit控件,分别为le_1,le_2,le_3
和一个QPushButton控件btn_1;
现在想通过单击btn_1这个按钮,来取出le_1和le_2中的内容,设置到le_3里去?
connect这个怎么写?
connect(btn_1,SIGNAL(clicked),le_3,SLOT(?))
SLOT 中写什么函数?
还有QLineEdit控件返回的是QString 类型的值,如果想把他变成整形或者浮点型,有什么方法?
谢谢大家,本人才接触Linux 和 QT 一天,有些东西实在搞得比较晕,烦请路过各位大虾帮忙,不胜感激.
一个QDialog上有三个QLineEdit控件,分别为le_1,le_2,le_3
和一个QPushButton控件btn_1;
现在想通过单击btn_1这个按钮,来取出le_1和le_2中的内容,设置到le_3里去?
connect这个怎么写?
connect(btn_1,SIGNAL(clicked),le_3,SLOT(?))
SLOT 中写什么函数?
还有QLineEdit控件返回的是QString 类型的值,如果想把他变成整形或者浮点型,有什么方法?
谢谢大家,本人才接触Linux 和 QT 一天,有些东西实在搞得比较晕,烦请路过各位大虾帮忙,不胜感激.
|
#include
#include
#include
#include
#include
#include
#include
#include
class SCalc : public QWidget
{
Q_OBJECT
public:
SCalc(QWidget *parent = 0,const char *name);
~SCalc();
private:
QPushButton * btn_calc;
QLabel * lb_title;
QLabel * lb_add;
QLabel * lb_equal;
QLineEdit * le_bjs;
QLineEdit * le_js;
QLineEdit * le_result;
}
SCalc::SCalc(QWidget *parent,const char *name)
: QWidget(parent,name)
{
setMaximumSize(QSize(400,80));
QToolTip:add(this,tr("copyright @ cprcf"));
QBoxLayout *vlayout = new QVBoxLayout(this,3);
lb_title->setText("A Simple Calculator");
vlayout->addWidget(lb_title);
lb_title->setAlignment(AlignHCenter | AlignTop);
lb_title->setFont(QFont("Times",20,QFont::Bold));
QBoxLayout *hlayout = new QHBoxLayout(vlayout);
le_bjs = new QLineEdit(this);
le_bjs->setMaxLength(10);
le_bjs->setValidator(new QDoubleValidator(le_bjs));
QToolTip::add(le_bjs,tr("Input Augend Here"));
hlayout->addWidget(le_bjs,5);
lb_add = new QLabel(this);
lb_add->setText("+");
hlayout->addWidget(lb_add,5);
le_js = new QLineEdit(this);
le_js->setMaxLength(10);
le_js->setValidator(new QDoubleValidator(le_js));
QToolTip::add(le_js,tr("Input Addend Here"));
hlayout->addWidget(le_js,5);
lb_equal = new QLabel(this);
lb_equal->setText("=");
hlayout->addWidget(lb_equal,5);
le_result = new QLineEdit(this);
le_result->setReadOnly(true);
hlayout->addWidget(le_result,5);
btn_calc = new QPushButton(this);
btn_calc->setText("calc");
QToolTip::add(btn,tr("click me"));
vlayout->addWidget(btn_calc);
vlayout->activate();
connect(btn_calc,SIGNAL(clicked()),le_result,SLOT());
}
SCalc::~SCalc()
{
}
int main(int argc,char **argv)
{
QApplication app(argc,argv);
SCalc *calc = new SCalc();
app.setMainWidget(calc);
calc->show();
return app.exec();
}
//下面是错误信息
本来开头没加Q_OBJECT 编译能通过 可以运行的
加了Q_OBJECT后 出现 如下错误提示
SCalc.o:In function 'SCalc::SCalc(QWidget *,char const *)':
SCalc.o(.text+0x29): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x48): undefined reference to 'SCalc::QPaintDevice virtual table'
SCalc.o(.text+0x7c): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x8c): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x29a): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x402): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x5c6): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o:In function 'SCalc::~SCalc(void)':
SCalc.o(.text+0x691): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x698): undefined reference to 'SCalc::QPaintDevice virtual table'
#include
#include
#include
#include
#include
#include
#include
class SCalc : public QWidget
{
Q_OBJECT
public:
SCalc(QWidget *parent = 0,const char *name);
~SCalc();
private:
QPushButton * btn_calc;
QLabel * lb_title;
QLabel * lb_add;
QLabel * lb_equal;
QLineEdit * le_bjs;
QLineEdit * le_js;
QLineEdit * le_result;
}
SCalc::SCalc(QWidget *parent,const char *name)
: QWidget(parent,name)
{
setMaximumSize(QSize(400,80));
QToolTip:add(this,tr("copyright @ cprcf"));
QBoxLayout *vlayout = new QVBoxLayout(this,3);
lb_title->setText("A Simple Calculator");
vlayout->addWidget(lb_title);
lb_title->setAlignment(AlignHCenter | AlignTop);
lb_title->setFont(QFont("Times",20,QFont::Bold));
QBoxLayout *hlayout = new QHBoxLayout(vlayout);
le_bjs = new QLineEdit(this);
le_bjs->setMaxLength(10);
le_bjs->setValidator(new QDoubleValidator(le_bjs));
QToolTip::add(le_bjs,tr("Input Augend Here"));
hlayout->addWidget(le_bjs,5);
lb_add = new QLabel(this);
lb_add->setText("+");
hlayout->addWidget(lb_add,5);
le_js = new QLineEdit(this);
le_js->setMaxLength(10);
le_js->setValidator(new QDoubleValidator(le_js));
QToolTip::add(le_js,tr("Input Addend Here"));
hlayout->addWidget(le_js,5);
lb_equal = new QLabel(this);
lb_equal->setText("=");
hlayout->addWidget(lb_equal,5);
le_result = new QLineEdit(this);
le_result->setReadOnly(true);
hlayout->addWidget(le_result,5);
btn_calc = new QPushButton(this);
btn_calc->setText("calc");
QToolTip::add(btn,tr("click me"));
vlayout->addWidget(btn_calc);
vlayout->activate();
connect(btn_calc,SIGNAL(clicked()),le_result,SLOT());
}
SCalc::~SCalc()
{
}
int main(int argc,char **argv)
{
QApplication app(argc,argv);
SCalc *calc = new SCalc();
app.setMainWidget(calc);
calc->show();
return app.exec();
}
//下面是错误信息
本来开头没加Q_OBJECT 编译能通过 可以运行的
加了Q_OBJECT后 出现 如下错误提示
SCalc.o:In function 'SCalc::SCalc(QWidget *,char const *)':
SCalc.o(.text+0x29): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x48): undefined reference to 'SCalc::QPaintDevice virtual table'
SCalc.o(.text+0x7c): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x8c): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x29a): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x402): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x5c6): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o:In function 'SCalc::~SCalc(void)':
SCalc.o(.text+0x691): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x698): undefined reference to 'SCalc::QPaintDevice virtual table'