1.程序可以显示中文
#include
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
#include
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
2.连接数据库//http://www.im80hou.com/html/mysql/2009/1003/1538.html
.pro中添加:QT += sql
.pro中添加:QT += sql
#include "QtSql/qsqldatabase.h"
#include
#define DB_SALES_DRIVER "QPSQL7"
#define DB_SALES_DBNAME "db"
#define DB_SALES_USER "user"
#define DB_SALES_PASSWD "pwd"
#define DB_SALES_HOST ""
QSqlDatabase db = QSqlDatabase::addDatabase ( DB_SALES_DRIVER);
db.setDatabaseName( DB_SALES_DBNAME );
db.setUserName( DB_SALES_USER );
db.setPassword( DB_SALES_PASSWD );
db.setHostName( DB_SALES_HOST );
if ( db.open() ) {
// 数据库被成功打开,我们现在可以运行SQL命令
cout<<"连接数据库成功"< }
else cout<<"连接数据库失败"<
sql语句:
QSqlQuery query;
query.exec("select short_name from merchants_def");
while(query.next())
{
QString name = query.value(1).toString();
printf ("name = %s\n", name.toUtf8().data());//toUtf8(), toAscii(), fromUtf8()
num++;
}
#include
#define DB_SALES_DRIVER "QPSQL7"
#define DB_SALES_DBNAME "db"
#define DB_SALES_USER "user"
#define DB_SALES_PASSWD "pwd"
#define DB_SALES_HOST ""
QSqlDatabase db = QSqlDatabase::addDatabase ( DB_SALES_DRIVER);
db.setDatabaseName( DB_SALES_DBNAME );
db.setUserName( DB_SALES_USER );
db.setPassword( DB_SALES_PASSWD );
db.setHostName( DB_SALES_HOST );
if ( db.open() ) {
// 数据库被成功打开,我们现在可以运行SQL命令
cout<<"连接数据库成功"<
else cout<<"连接数据库失败"<
sql语句:
QSqlQuery query;
query.exec("select short_name from merchants_def");
while(query.next())
{
QString name = query.value(1).toString();
printf ("name = %s\n", name.toUtf8().data());//toUtf8(), toAscii(), fromUtf8()
num++;
}
//动态
int a1234=10;
query.prepare("select * from merchants_def where merchant_no=:a1234");
query.bindValue(":a1234", a1234);
query.exec();
while(query.next()){
int x=query.value(0).toInt();
cout<<"x="< }
3.treewidget
QTreeWidgetItem *issuer1 = new QTreeWidgetItem(m_ui->treeWidget);
issuer1->setText(0, tr("发卡方1"));
QTreeWidgetItem *merchant1 = new QTreeWidgetItem(issuer1);
merchant1->setText(0, tr("商户1"));
merchant1->setText(1, tr("Yes"));
int a1234=10;
query.prepare("select * from merchants_def where merchant_no=:a1234");
query.bindValue(":a1234", a1234);
query.exec();
while(query.next()){
int x=query.value(0).toInt();
cout<<"x="<
3.treewidget
QTreeWidgetItem *issuer1 = new QTreeWidgetItem(m_ui->treeWidget);
issuer1->setText(0, tr("发卡方1"));
QTreeWidgetItem *merchant1 = new QTreeWidgetItem(issuer1);
merchant1->setText(0, tr("商户1"));
merchant1->setText(1, tr("Yes"));
QTreeWidgetItem *outlet1 = new QTreeWidgetItem(merchant1);
outlet1->setText(0, tr("网点1"));
outlet1->setText(1, tr("Yes"));
outlet1->setText(0, tr("网点1"));
outlet1->setText(1, tr("Yes"));
QTreeWidgetItem *outlet2 = new QTreeWidgetItem(merchant1);
outlet2->setText(0, tr("网点2"));
outlet2->setText(1, tr("Yes"));
outlet2->setText(0, tr("网点2"));
outlet2->setText(1, tr("Yes"));
QTreeWidgetItem *merchant2 = new QTreeWidgetItem(issuer1);
merchant2->setText(0, tr("商户2"));
merchant2->setText(1, tr("Yes"));
4.tablewidget(每次修改后重新显示表)
#include
显示table:
m_ui->tableWidget_2->setRowCount(2);
m_ui->tableWidget_2->setItem(0,1,new QTableWidgetItem(tr("123")));
m_ui->tableWidget_2->setItem(1,1,new QTableWidgetItem(tr("12")));
取某值:
g_select_key[j]=atoi(m_ui->tableWidget->item(temp[j],0)->text().toAscii().data());
选中行:
QItemSelectionModel *m = m_ui->tableWidget_2->selectionModel();
QModelIndexList indexes = m->selectedIndexes();
QModelIndex index;
merchant2->setText(0, tr("商户2"));
merchant2->setText(1, tr("Yes"));
4.tablewidget(每次修改后重新显示表)
#include
显示table:
m_ui->tableWidget_2->setRowCount(2);
m_ui->tableWidget_2->setItem(0,1,new QTableWidgetItem(tr("123")));
m_ui->tableWidget_2->setItem(1,1,new QTableWidgetItem(tr("12")));
取某值:
g_select_key[j]=atoi(m_ui->tableWidget->item(temp[j],0)->text().toAscii().data());
选中行:
QItemSelectionModel *m = m_ui->tableWidget_2->selectionModel();
QModelIndexList indexes = m->selectedIndexes();
QModelIndex index;
int temp[11]={0};
int which_row[11]={0};
int i=0;
int count_no = 10;
foreach(index,indexes){
int which_row[11]={0};
int i=0;
int count_no = 10;
foreach(index,indexes){
temp[i++]= index.row();
}
if(i==0)
QMessageBox::warning(this,tr("警告"),tr("请先选中一行"));
else
if(i==1)
{
which_row[0]=temp[0];
if(which_row[0]>=count_no)
QMessageBox::warning(this,tr("警告"),tr("选择有误"));
else
{
}
if(i==0)
QMessageBox::warning(this,tr("警告"),tr("请先选中一行"));
else
if(i==1)
{
which_row[0]=temp[0];
if(which_row[0]>=count_no)
QMessageBox::warning(this,tr("警告"),tr("选择有误"));
else
{
cout<<"修改。。。"< cout<<"which_row[0]="< m_ui->tableWidget_2->setRowCount(3);
m_ui->tableWidget_2->setItem(0,1,new QTableWidgetItem(tr("123")));
m_ui->tableWidget_2->setItem(1,1,new QTableWidgetItem(tr("12")));
m_ui->tableWidget_2->setItem(2,1,new QTableWidgetItem(tr("12")));
}
}
else
if(i>1)
{
int j;
for(j=1;j {
if(temp[j]==temp[0])
break;
}
if(j==1)
{
which_row[0]=temp[0];
if(which_row[0]>=count_no)
QMessageBox::warning(this,tr("警告"),tr("选择有误"));
else
{
cout<<"修改。。。"< cout<<"which_row[0]="< m_ui->tableWidget_2->setRowCount(3);
m_ui->tableWidget_2->setItem(0,1,new QTableWidgetItem(tr("123")));
m_ui->tableWidget_2->setItem(1,1,new QTableWidgetItem(tr("12")));
m_ui->tableWidget_2->setItem(2,1,new QTableWidgetItem(tr("12")));
}
}
else
QMessageBox::warning(this,tr("警告"),tr("一次只能修改一行"));
}
m_ui->tableWidget_2->setItem(0,1,new QTableWidgetItem(tr("123")));
m_ui->tableWidget_2->setItem(1,1,new QTableWidgetItem(tr("12")));
m_ui->tableWidget_2->setItem(2,1,new QTableWidgetItem(tr("12")));
}
}
else
if(i>1)
{
int j;
for(j=1;j {
if(temp[j]==temp[0])
break;
}
if(j==1)
{
which_row[0]=temp[0];
if(which_row[0]>=count_no)
QMessageBox::warning(this,tr("警告"),tr("选择有误"));
else
{
cout<<"修改。。。"<
m_ui->tableWidget_2->setItem(0,1,new QTableWidgetItem(tr("123")));
m_ui->tableWidget_2->setItem(1,1,new QTableWidgetItem(tr("12")));
m_ui->tableWidget_2->setItem(2,1,new QTableWidgetItem(tr("12")));
}
}
else
QMessageBox::warning(this,tr("警告"),tr("一次只能修改一行"));
}
else
return;
5.comboBox
#include
for( i=0; i ui->merchant_comboBox->addItem(QString::fromUtf8(short_name[i]));
merchant_name=ui->merchant_comboBox->currentText();//comboBox
start_date=ui->start_dateEdit->text();//dateEdit
end_date=ui->end_dateEdit->text();
report_type=ui->type_comboBox->currentText();
s=ui->merchants_comboBox->itemText(i);//
return;
5.comboBox
#include
for( i=0; i
merchant_name=ui->merchant_comboBox->currentText();//comboBox
start_date=ui->start_dateEdit->text();//dateEdit
end_date=ui->end_dateEdit->text();
report_type=ui->type_comboBox->currentText();
s=ui->merchants_comboBox->itemText(i);//
6.登录框( http://hi.baidu.com/yafeilinux/blog/item/a335cd10a465d174cb80c42d.html)
QApplication a(argc, argv);
Widget w;
loginDlg login;
if(login.exec()==QDialog::Accepted)
{
w.show();
return a.exec();
}
else return 0;
QApplication a(argc, argv);
Widget w;
loginDlg login;
if(login.exec()==QDialog::Accepted)
{
w.show();
return a.exec();
}
else return 0;
//
if(m_ui->usrlineEdit->text()==tr("hqq")&&m_ui->pwdlineEdit->text()==tr("hqq"))
if(m_ui->usrlineEdit->text()==tr("hqq")&&m_ui->pwdlineEdit->text()==tr("hqq"))
//判断用户名和密码是否正确
accept();
else{
QMessageBox::warning(this,tr("Warning"),tr("user name or password error!"),QMessageBox::Yes);
//如果不正确,弹出警告对话框
m_ui->usrlineEdit->clear();//清空用户名输入框
m_ui->pwdlineEdit->clear();//清空密码输入框
m_ui->usrlineEdit->setFocus();//将光标转到用户名输入框
}
7.横向滚动条和spinBox关联使用
ui->spinBox->setRange(0, 130);//设置各自的取值范围
ui->horizontalSlider->setRange(0, 130);
accept();
else{
QMessageBox::warning(this,tr("Warning"),tr("user name or password error!"),QMessageBox::Yes);
//如果不正确,弹出警告对话框
m_ui->usrlineEdit->clear();//清空用户名输入框
m_ui->pwdlineEdit->clear();//清空密码输入框
m_ui->usrlineEdit->setFocus();//将光标转到用户名输入框
}
7.横向滚动条和spinBox关联使用
ui->spinBox->setRange(0, 130);//设置各自的取值范围
ui->horizontalSlider->setRange(0, 130);
//滑块和Spin组件的值的变化都会对应的改变
QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)),
ui->horizontalSlider, SLOT(setValue(int)));
QObject::connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
ui->spinBox, SLOT(setValue(int)));
QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)),
ui->horizontalSlider, SLOT(setValue(int)));
QObject::connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
ui->spinBox, SLOT(setValue(int)));
ui->spinBox->setValue(35);//注意这里的设置也会影响slider
8.progressbar的使用(点击按钮触发progressbar,而不是触发整个界面)
for (i=1;i<=100;i++)
{
m_ui->progressbar->setValue(i);
for ( j=0; j<1000*1000*4;j++)
{
}
for (i=1;i<=100;i++)
{
m_ui->progressbar->setValue(i);
for ( j=0; j<1000*1000*4;j++)
{
}
}
9.mdi:在Qt中要制作MDI介面的视窗,是使用QMainWindow ,并将其中心元件(Central Widget)设为QMdiArea实例,而每一个MDI子视窗,则使用QMdiArea的addSubWindow()来加入。
#include
在on_actionNew_N_triggered()中:
QMdiArea *mdiArea = new QMdiArea;
this->setCentralWidget(mdiArea);
QTextEdit *textEdit = new QTextEdit;
textEdit->setWindowTitle("File1");
mdiArea->addSubWindow(textEdit);
10.connect(sender, signal, receiver, member)
sender是发生器,signal是发出的信号,receiver是接收器,member是槽。
如:connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enableGetFortuneButton()));
11.设置可编辑状态
for(j=0;j m_ui->tableWidget->item(i,19)->setFlags(Qt::ItemIsEditable);
12.QMessageBox
if(QMessageBox::information(this,tr("警告"),tr("确定删除?"),QMessageBox::No,QMessageBox::Yes)==16384)
#include
在on_actionNew_N_triggered()中:
QMdiArea *mdiArea = new QMdiArea;
this->setCentralWidget(mdiArea);
QTextEdit *textEdit = new QTextEdit;
textEdit->setWindowTitle("File1");
mdiArea->addSubWindow(textEdit);
10.connect(sender, signal, receiver, member)
sender是发生器,signal是发出的信号,receiver是接收器,member是槽。
如:connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enableGetFortuneButton()));
11.设置可编辑状态
for(j=0;j
12.QMessageBox
if(QMessageBox::information(this,tr("警告"),tr("确定删除?"),QMessageBox::No,QMessageBox::Yes)==16384)
13.QFile
QFile file("/home/hqq/1.txt");
char *msg="1234";
file.open( QIODevice::WriteOnly);
file.write(msg, qstrlen(msg));
file.close();
QFile file("/home/hqq/1.txt");
char *msg="1234";
file.open( QIODevice::WriteOnly);
file.write(msg, qstrlen(msg));
file.close();
14.execl
QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),"",tr("file(*.CSV)"));
cout<<"fileName="< if(fileName == "")
return;
QTextCodec *code;
code = QTextCodec::codecForName("gb18030");
QString fileName=QFileDialog::getSaveFileName(this,tr("Save File"),"",tr("file(*.CSV)"));
cout<<"fileName="<
return;
QTextCodec *code;
code = QTextCodec::codecForName("gb18030");
std::string strbuffer = code->fromUnicode(fileName).data();
FILE *fileWrite = fopen( strbuffer.c_str(),"w+");
QString strFemale = "Female ";
QString strMale = "Male ";
QString strPatientCount = "Patient ";
char a[20]="abc";
char b[20]="一二三";
QString str ="\n";
//std::string strCountbuffer = code->fromUnicode(strFemale+","+strMale+","+strPatientCount+str+QString().setNum(femaleCount)+","+QString().setNum(maleCount)+","+QString().setNum(patientCount)).data();
//std::string strCountbuffer=code->fromUnicode(strFemale+","+strMale+","+strPatientCount+str+QString().setNum(1)+","+QString().setNum(1)+","+QString().setNum(1)).data();
//std::string strCountbuffer=code->fromUnicode(strFemale+strMale+strPatientCount+str+QString().setNum(1)).data();//可用
std::string strCountbuffer=code->fromUnicode(strFemale+strMale+strPatientCount+str+a+' '+tr(b)).data();
QFile file;
file.open(fileWrite, QIODevice::WriteOnly);
file.write(strCountbuffer.c_str(), qstrlen(strCountbuffer.c_str()));
file.close();
FILE *fileWrite = fopen( strbuffer.c_str(),"w+");
QString strFemale = "Female ";
QString strMale = "Male ";
QString strPatientCount = "Patient ";
char a[20]="abc";
char b[20]="一二三";
QString str ="\n";
//std::string strCountbuffer = code->fromUnicode(strFemale+","+strMale+","+strPatientCount+str+QString().setNum(femaleCount)+","+QString().setNum(maleCount)+","+QString().setNum(patientCount)).data();
//std::string strCountbuffer=code->fromUnicode(strFemale+","+strMale+","+strPatientCount+str+QString().setNum(1)+","+QString().setNum(1)+","+QString().setNum(1)).data();
//std::string strCountbuffer=code->fromUnicode(strFemale+strMale+strPatientCount+str+QString().setNum(1)).data();//可用
std::string strCountbuffer=code->fromUnicode(strFemale+strMale+strPatientCount+str+a+' '+tr(b)).data();
QFile file;
file.open(fileWrite, QIODevice::WriteOnly);
file.write(strCountbuffer.c_str(), qstrlen(strCountbuffer.c_str()));
file.close();
15.打印
#include
#include
#include
#include
#include
QTextDocument *document = ui->textEdit->document();
QPrinter printer;
#include
#include
#include
#include
#include
QTextDocument *document = ui->textEdit->document();
QPrinter printer;
QPrintDialog dlg(&printer, this);
dlg.setWindowTitle(tr("打印文档"));
if (dlg.exec() != QDialog::Accepted)
return;
dlg.setWindowTitle(tr("打印文档"));
if (dlg.exec() != QDialog::Accepted)
return;
document->print(&printer);