76.QT槽的机制
- 按钮点击获取文本框输入
void Dialog::on_pushButton_clicked()
{
//获取文本输入
QString vstr = ui->lineEdit->text(); //判断是否转换成功
bool isok;
//转换
int value = vstr.toInt(&isok);
double area = value*value*PI;
QString tempstr;
ui->lineEdit_2->setText(tempstr.setNum(area));
} - 文本框内容改变执行的操作
void Dialog::on_lineEdit_textChanged(const QString &arg1)
{
this->on_pushButton_clicked();
}
手动实现QT的界面
- dialog.h添加按钮,文本框,标签等头文件
#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel> - 在dialog.h类中添加按钮,文本框,标签的指针,并添加按钮的触发函数
private:
Ui::Dialog *ui;
//图形控件
QLabel *label1,*label2;
QLineEdit *edit1,*edit2;
QPushButton *button; //触发机制
private slots:
void showit(); - 在dialog.cpp中初始化,并创建布局
- 创建布局头文件
#include "QGridLayout"//布局
- 初始化
//加上this显示在当前窗口
button = new QPushButton(this);
button->setText(tr("计算"));
//button->show();
button->resize(,); edit1 = new QLineEdit(this); edit2 = new QLineEdit(this); label1 = new QLabel(this); label1->setText("输入半径:"); label2 = new QLabel(this); label2->setText("输出面积:"); - 创建布局
//网格布局
QGridLayout *mylayout = new QGridLayout(this);
mylayout->addWidget(label1,,);
mylayout->addWidget(label2,,);
mylayout->addWidget(edit1,,);
mylayout->addWidget(edit2,,);
mylayout->addWidget(button,,); - 关联按钮的函数或者文本框变化的函数
//clicked触发 关联
connect(button,SIGNAL(clicked(bool)),this,SLOT(showit()));
connect(edit1,SIGNAL(textChanged(QString)),this,SLOT(showit())); - 按钮点击函数实现
void Dialog::showit()
{
//获取文本输入
QString vstr = edit1->text(); //判断是否转换成功
bool isok;
//转换
int value = vstr.toInt(&isok);
double area = value*value*PI;
QString tempstr;
edit2->setText(tempstr.setNum(area));
}
完整代码:
- dialog.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel> namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private:
Ui::Dialog *ui;
//图形控件
QLabel *label1,*label2;
QLineEdit *edit1,*edit2;
QPushButton *button; //触发机制
private slots:
void showit();
}; #endif // DIALOG_H - dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "QGridLayout"//布局
#define PI 3.14159 Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
//加上this显示在当前窗口
button = new QPushButton(this);
button->setText(tr("计算"));
//button->show();
button->resize(,); edit1 = new QLineEdit(this); edit2 = new QLineEdit(this); label1 = new QLabel(this); label1->setText("输入半径:"); label2 = new QLabel(this); label2->setText("输出面积:"); //网格布局
QGridLayout *mylayout = new QGridLayout(this);
mylayout->addWidget(label1,,);
mylayout->addWidget(label2,,);
mylayout->addWidget(edit1,,);
mylayout->addWidget(edit2,,);
mylayout->addWidget(button,,); //clicked触发 关联
connect(button,SIGNAL(clicked(bool)),this,SLOT(showit()));
connect(edit1,SIGNAL(textChanged(QString)),this,SLOT(showit()));
ui->setupUi(this);
} Dialog::~Dialog()
{
delete ui;
} void Dialog::showit()
{
//获取文本输入
QString vstr = edit1->text(); //判断是否转换成功
bool isok;
//转换
int value = vstr.toInt(&isok);
double area = value*value*PI;
QString tempstr;
edit2->setText(tempstr.setNum(area));
} - main.cpp
#include "dialog.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show(); return a.exec();
}
76.QT槽的机制的更多相关文章
- qt信号signal和槽slot机制
内容: 一.概述 二.信号 三.槽 四.信号与槽的关联 五.元对象工具 六.程序样例 七.应注意的问题 信号与槽作为QT的核心机制在QT编程中有着广泛的应用,本文介绍了信号与槽的一些基本概念.元对象工 ...
- 第七章 探秘Qt的核心机制-信号与槽
第七章 探秘Qt的核心机制-信号与槽 注:要想使用Qt的核心机制信号与槽,就必须在类的私有数据区声明Q_OBJECT宏,然后会有moc编译器负责读取这个宏进行代码转化,从而使Qt这个特有的机制得到使用 ...
- Qt 内存管理机制(转)
许转载http://devbean.blog.51cto.com/448512/526734 强类型语言在创建对象时总会显式或隐式地包含对象的类型信息.也就是说,强类型语言在分配对象内存空间时,总 ...
- 剖析Qt的事件机制原理
版权声明 请尊重原创作品.转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正. QT源码解析(一) QT创建窗口程序.消息循环和WinMai ...
- Qt 内存管理机制
这篇文章首先发布于我的主页 http://www.devbean.info,以后也会直接发布在那里.现在有 Flex 4 的一篇和 <从 C++ 到 Objective-C>系列,感谢大家 ...
- Qt之事件处理机制
思维导读 一.事件简介 QT程序是事件驱动的, 程序的每个动作都是由内部某个事件所触发.QT事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. 常见的QT事件类型如下: 键盘事件: 按键按下 ...
- 回调函数实现类似QT中信号机制(最简单)
1. 定义回调接口类: class UIcallBack{public: virtual void onAppActivated() = 0; virtual void onShowMore() = ...
- 深入Qt 学习 -- 反射机制(比较简单清楚)
相对于Java天生的这一特性, C++并不具备;但进入到Qt领域,这一切都变得简单自如了. 从Qt的元对象系统可知,除了提供信号/槽机制的特性之外,它还提供了以下特性: ■ QObject::meta ...
- 回调函数实现类似QT中信号机制
1. 定义回调接口类: class UIcallBack { public: virtual void onAppActivated() = 0; virtual void onShowMore() ...
随机推荐
- 工作日志:dispatch_once、网络缓存、分享问题
问题描述一: 每一个接口对应的model的数据在进入对应的模块(视图)时,执行一次本地缓存加载: 执行缓存加载的实现在基类实现. 解决方案: 将dispatch_once_t设置为成员变量: 问题扩展 ...
- mysql更改密码与远程管理
set password = ': #在当前用户下更改密码 grant all privileges on *.* to root@"%" identified by " ...
- caioj 1074 动态规划入门(中链式1:最小交换合并问题)
经典的石子合并问题!!! 设f[i][j]为从i到j的最大值 然后我们先枚举区间大小,然后枚举起点终点来更新 f[i][j] = min(f[i][k] + f[k+1][j] + sum(i, j) ...
- HNU 12961 BitTorrent DP
题意: 你在网上下载东西,一个文件存储在一段或者多段里面,问怎么选择能在规定的流量内下载最多的文件数量.每段的大小一样. 思路: 习惯了做答案保存在DP数组里的题,做这种答案保存在下标里的题,转不过弯 ...
- 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
- HTML——上中下布局
上中下布局是最主要的布局方式,本比如果用户屏幕分辨率为800*600像素. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vuc2h1bWlu/font ...
- android-继承BaseAdapter--自己定义适配器,getView运行多次的解决方法
定义的getView运行多次的ListView布局: <ListView android:id="@+id/lv_messages" android:layout_width ...
- 使用ssh过程中对数据库进行update时报错
报错信息:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in ...
- CentOS 中使用yum出现的“UnicodeDecodeError: 'ascii' codec”问题解决方法
问题 新装了CentOS 6.5系统,打算使用yum安装程序是出现了例如以下错误: Loading mirror speeds from cached hostfile Traceback (most ...
- thinkphp中cookie和session中操作数组的方法
thinkphp中cookie和session中操作数组的方法 一.ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况 在ThinkPHP的模板中操作sessio ...