Qt之TableWidget、信号、槽

来源:互联网 发布:阿里云配置vpn 编辑:程序博客网 时间:2024/04/29 22:12

一、TableWidget组件 用法

案例:----在表格上,选取一行后,可将该行的信息读取并显示到 LineEdit等组件上,便于后续的 修改操作步骤:1、在表格上 点击右键----Go to Slot----选Clicked2、会自动产生 单击触发的函数:void TableWidget01Widget::on_tableWidget_clicked(QModelIndex index){ if(!ui->tableWidget->isItemSelected(ui->tableWidget->currentItem()))//如果没有选任何行 也不能删除 return; int row=ui->tableWidget->currentRow();//获取当前的 行号 if(row<0)//保护措施,行号小于0,说明没有行数据,就不要删了,否则程序会崩溃 return; QTableWidgetItem *tableWidgetNumber=ui->tableWidget->item(row,0);//读取当前行的0列数据,注意数据类型是 QTableWidgetItem * //tableWidgetNumber->text(); ui->lineEditNumber->setText(tableWidgetNumber->text());//设置LineEdit组件里的值}

二、信号、槽

使用系统已有的信号:方法:我们前面使用的方法,即 按钮上点右键----go to Slot自定义信号、槽函数即: 手工建立 信号 手工建立 槽函数 在构造函数中 connect 信号 和 槽案例:----点击 按钮后,Label中的文字被修改为 Hello World步骤:1、修改h文件:增加 信号的原型增加 槽函数的原型#ifndef SIGNALSLOT001WIDGET_H#define SIGNALSLOT001WIDGET_H#include <QtCore>#include <QtGui>namespace Ui{ class SignalSlot001Widget;}class SignalSlot001Widget : public QWidget{ Q_OBJECTpublic: SignalSlot001Widget(QWidget *parent = 0); ~SignalSlot001Widget();private: Ui::SignalSlot001Widget *ui;private slots: void on_pushButtonModify2_clicked(); void on_pushButtonModify_clicked(); void slotModifyLabel(QString str);//这1行,增加 槽函数slotModifyLabel的原型signals://这2行,增加了 信号signalModifyLabel的原型 void signalModifyLabel(QString str);};#endif // SIGNALSLOT001WIDGET_H2、cpp中,实现槽函数;在构造函数中connect信号--槽:SignalSlot001Widget::SignalSlot001Widget(QWidget *parent) : QWidget(parent), ui(new Ui::SignalSlot001Widget){ ui->setupUi(this); connect(this,SIGNAL(signalModifyLabel(QString)),this,SLOT(slotModifyLabel(QString)));//发送者 接收者------ 与信号函数、槽函数所在的对象地址 一致}void SignalSlot001Widget::slotModifyLabel(QString str){ qDebug()<<"enter 1"; ui->label->setText(str); ui->label->adjustSize(); return;}

三、使用自定义的 信号----槽

案例:使用上面定义的信号、槽,要求点击 修改按钮,可以修改Label的值信号signalModifyLabel槽函数slotModifyLabel步骤:1、在按钮上 点击 右键----Go to Slot2、在自动生成的槽函数中,增加 发送信息的语句void SignalSlot001Widget::on_pushButtonModify2_clicked(){ emit signalModifyLabel("Hello,world");//发送 自定义信号,该信号还带有参数 return;}结果:由于构造函数中,signalModifyLabel与slotModifyLabel 已经关联,因此,信号发出后,slotModifyLabel函数会被执行,该函数修改Label了

四、 QString的操作 qDebug()的用法

QString存储了 1串 QChar每个QChar 为UNICODE字符----16位编辑查询转换QString的编辑操作,代码:空白字符:\r \n \t \f \v ' '#include <QCoreApplication>#include <QDebug>#include <QStringList>#include <QVector>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QString str="hello孙洪"; qDebug()<<str.size();//返回字符个数 str[0]=QChar('H');//修改QString中的 某个字符----注意是 QChar类型的 字符 str.append("china");//追加 str.insert(5,",sunhong,");//插入 str.replace(0,5,"你好");//替换 str+="!!!";//字符串 连接 str=" hi\r\n Qt \n "; qDebug()<<str; qDebug()<<str.trimmed();//去掉字符串 2边的 空白字符 qDebug()<<str.simplified();//内部多余的 空白字符 也去掉了 str="one,two,three,,,"; QStringList stringList=str.split(',',QString::SkipEmptyParts);//将字符串 根据分隔符 ,分离为多个,然后放到 QStringList中 qDebug()<<stringList; QStringList stringList1; stringList1<<"good"<<"morning"<<"Mr.sun"; QString str2=stringList1.join(':');//将QStringList中的元素,连接起来---可以指定 连接字符 qDebug()<<str2; qDebug()<<QString("").isNull(); qDebug()<<QString("").isEmpty(); QString str4; qDebug()<<str4.isNull();//未初始化的字符串,isNull return 0;}QString的查询操作:字符串的比较: < > compare函数代码:#include <QCoreApplication>#include <QDebug>#include <QStringList>#include <QVector>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QString str1="tom is studying English"; qDebug()<<str1.left(3); qDebug()<<str1.right(7); qDebug()<<str1.mid(4,2); qDebug()<<str1.indexOf("is"); qDebug()<<str1.at(5); qDebug()<<str1.count('s'); qDebug()<<str1.contains("study"); qDebug()<<str1.startsWith("tom"); qDebug()<<str1.endsWith("English"); return 0;}QString的转换操作:#include <QCoreApplication>#include <QDebug>#include <QStringList>#include <QVector>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QString str1="99"; bool flag=false; int str1_int1=str1.toInt(&flag);//字符串转为整数,注意:要检查 flag qDebug()<<flag<<endl<<str1_int1; int num=1001; QString num_str1=QString::number(num);//数字---->字符串 qDebug()<<num_str1; QString str2="china"; str2.toUpper();//转换为 大写 qDebug()<<str2; QString name="zhangsan"; int age=21; QString str3=QString("name is %1 age is %2").arg(name).arg(age);//arg函数的使用 qDebug()<<"str3 is:"<<str3; qDebug()<<name; qDebug()<<qPrintable(name);//将 字符串 转为 char * return 0;}

五、QByteArray----字节数组

在后台,总是保证 \0 结尾隐式共享类比较:QString字符都是 UNICODE字符,为16位---方便存储 非ASCII字符、汉字等二者 接口函数 几乎相同六、QVariant是QtCore中的类,可以toInt toFloatQtGui中的数据类型,QColor QImage 就不能转换了代码:建立1个 GUI应用,在构造函数中 添加代码Temp05Widget::Temp05Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Temp05Widget){ ui->setupUi(this); QVariant a=10; bool flag=false; int a_int=a.toInt(&flag); //qDebug()<<a_int; //qDebug()<<flag; QColor color=QColor(Qt::red); a=color; qDebug()<<a.type(); qDebug()<<a.value<QColor>();}

七、使用QStringList

 简化ComboBox和TableWidget的初始化过程修改cpp的构造函数:TableWidget01Widget::TableWidget01Widget(QWidget *parent)        : QWidget(parent), ui(new Ui::TableWidget01Widget){    ui->setupUi(this);    //ui->comboBoxSex->addItem("男");    //ui->comboBoxSex->addItem("女");    QStringList texts;    texts<<"男1"<<"女2";    ui->comboBoxSex->addItems(texts);  //利用QStringList类型的参数,进行了简化    //ui->tableWidget->setRowCount(2);    ui->tableWidget->setColumnCount(3);    //QTableWidgetItem *number=new QTableWidgetItem("number");    //QTableWidgetItem *name=new QTableWidgetItem("name");    //QTableWidgetItem *sex=new QTableWidgetItem("sex");    //ui->tableWidget->setHorizontalHeaderItem(0,number);    //ui->tableWidget->setHorizontalHeaderItem(1,name);    //ui->tableWidget->setHorizontalHeaderItem(2,sex);    QStringList labels;    labels<<"number"<<"name"<<"sex";    ui->tableWidget->setHorizontalHeaderLabels(labels);//利用QStringList类型的参数,进行了简化    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);    /*QLabel *labelNumber=new QLabel("1001");    QLabel *labelName=new QLabel("zhangsan");    QComboBox *comboBoxSex=new QComboBox;    comboBoxSex->addItem("man");    comboBoxSex->addItem("woman");    ui->tableWidget->setCellWidget(0,0,labelNumber);    ui->tableWidget->setCellWidget(0,1,labelName);    ui->tableWidget->setCellWidget(0,2,comboBoxSex);    ui->tableWidget->resizeColumnsToContents();    ui->tableWidget->resizeRowsToContents();*/    //ui->tableWidget->show();}   

0 0
原创粉丝点击