一、复选框
复选框的操作函数
bool isChecked() const //判断这个复选框是否被选中
void setChecked(bool) //设置这个复选框的选中情况
复选框的信号
void stateChanged(int state) //状态改变信号
例1创建一个是否同意协议的复选框选中后下一步按钮能用不选中下一步按钮不能用
#ifndef TEST_H
#define TEST_H
#include <QMainWindow>
#include<QWidget>
#include<QPushButton>
#include<QLineEdit>
#include<QPlainTextEdit>
#include<QChecBox>
class test : public QMainWindow
{
Q_OBJECT
public:
explicit test(QWidget *parent = 0);
~test();
private:
QPushButton* m_button;
QPushButton* next_button;
QPlainTextEdit* m_textEdit;
QCheckBox* cbox; //定义一个复选框的变量
private slots:
int button_click(); //
int check_changed();
};
#endif // TEST_H
#include "test.h"
#include "ui_test.h"
test::test(QWidget *parent) :
QMainWindow(parent)
{
m_textEdit=new QPlainTextEdit(this); //多行文本框
m_textEdit->setGeometry(20,20,300,200); //设置位置和大小
cbox=new QCheckBox(this); //复选框
cbox->setText("yes/not"); /./复选框上的文本
cbox->setGeometry(20,230,90,40);
m_button=new QPushButton(this);//按钮
m_button->setText("cancle");
m_button->setGeometry(180,230,70,40);
next_button=new QPushButton(this);
next_button->setText("next");
next_button->setGeometry(250,230,70,40);
next_button->setEnabled(false);//失能next的按钮
connect(m_button,SIGNAL(clicked()),this,SLOT(button_click()));
connect(cbox,SIGNAL(stateChanged(int)),this,SLOT(check_changed()));//把复选框改变发出的信号和要做的动作的槽连接起来
}
test::~test()
{
}
int test::button_click()
{
m_textEdit->setPlainText("hello,world");
return 0;
}
int test::check_changed()
{
if(cbox->isChecked())
{
next_button->setEnabled(true);//使能next的按钮
return 1;
}
else
{
next_button->setEnabled(false);
return 0;
}
}
二、单行文本框
#include<QLineEdit>//添加头文件
QLineEdit *m_line;//在界面类头文件中中定义一个变量
m_line=new QLineEdit(this);//构造函数中给单行变量申请空间
m_line->setText("cancle");//添加行中的文字
m_line->setGeometry(180,230,70,40);//设置行的位置和大小
QString tex=m_line->text();//获得行中文字给tex
#include<QMessageBox>//提示框的头文件
QMessageBox::information(this,"ok","good input");//ok是框名 goodinput是提示内容
connect(m_line,SIGNAL(returnPressed()),this,SLOT(OnReturn()));//把回车信号和OnReturn()这个函数连接起来,当在m_line上按下回车是会触发这个函数
三、组合框
#include<QComboBox>//添加组合框的头文件
QConboBox* com_box;//在头文件中添加一个组合框的变量
com_box=new QComboBox(this);//创建一个组合框
com_box->addItem("Chinese");
com_box->addItem("English");
com_box->addItem("French");/./依次在组合框下添加选项
com_box->insertItem(2,"Koern");//在指定位置添加一个选项
com_box->addItem("Chinese","ch");
com_box->addItem("English","en");
com_box->addItem("French","er");
com_box->insertItem(2,"Koern","ko");//后面添加的缩写是前面信息的关联数据data也可以为数字
int index=com_box->currentIndex();//得到当前选项的位置
QString data=com_box->itemData(index),toString();//得到当前选项的关联数据data,
QString text=com_box->itemText(index);//得到当前选项的值
com_box->setEditable(true);//这样组合框不仅可以选择也可以编辑
QString value=com_box->currentText();//可以得到编辑后的选项
connect(com_box,SIGNAL(currentIndexChanged(int)),this,SLOT(OnChanged(int)));//如果所选的组合框中的选项发生改变就会运行OnChanged()函数,并且会吧变后的位置传给OnChenged()函数