Qt使用中遇到的问题及解决方案

1. 解决QSqlDatabasePrivate::removeDatabase: connection ‘myConnection’ is still in use, all queries will cease to work的问题

该问题主要是因为没有关闭之前的数据库连接,然后又需要创建新的数据库连接导致。解决方案:必须释放该连接的所有查询,即删除所有与该连接有关的query;同时在释放该连接时,需要先获取该连接的连接名,然后先关闭当前数据库,并将当前数据库使用默认构造函数覆盖,最后再移除当前连接。
解决代码:
void database::close()
{
QString connection;
connection = m_db.connectionName();
m_db.close();
m_db = QSqlDatabase();
m_db.removeDatabase(connection);
}

参考链接:http://stackoverflow.com/questions/9519736/warning-remove-database
http://stackoverflow.com/questions/8461333/sql-connection-still-open-even-after-deleting-it

2.解决Attempting to add QLayout “” to QDialog “”, which already has a layout的问题

该问题主要是由于将QDialog对象同时加入到了多个布局中间导致的,解决方案是在创建子布局时不要设置默认部件。即在创建布局时采用默认构造函数,不要设置部件。
解决代码:

    QDialog* dlg = new QDialog(this);
    //输入框
    serverLineEdit = new QLineEdit(dlg);
    //文本提示标签
    QLabel* serverLabel = new QLabel(dlg);

    //按钮
    QPushButton *okBtn = new QPushButton(dlg);
    QPushButton *cancelBtn = new QPushButton(dlg);


    okBtn->setText(QString("确定"));
    cancelBtn->setText(QString("取消"));
    serverLabel->setText(QString("server:"));

    //server栏布局
    QHBoxLayout* ServerHLayout = new QHBoxLayout;//不要设置参数!!!
    serverLabel->setFixedWidth(80);
    serverLineEdit->setFixedWidth(200);
    serverLineEdit->setText("127.0.0.1");
    ServerHLayout->addWidget(serverLabel);
    ServerHLayout->addWidget(serverLineEdit);

    QHBoxLayout* btnHLayoutBtn = new QHBoxLayout;//不要设置参数!!!
    btnHLayoutBtn->insertWidget(1,okBtn,0,Qt::AlignHCenter);
    btnHLayoutBtn->insertWidget(2,cancelBtn,0,Qt::AlignHCenter);

    QVBoxLayout *VLayout = new QVBoxLayout;//不要设置参数!!!
    VLayout->addLayout(ServerHLayout);
    VLayout->addLayout(btnHLayoutBtn);
    VLayout->setSpacing(20);

    QGridLayout* globLayout = new QGridLayout(dlg);//此处可以设置参数,顶级窗口
    globLayout->addLayout(VLayout,2,10);

    dlg->setLayout(globLayout);

    connect(okBtn,SIGNAL(clicked()),this,SLOT(getDBLoginInfo()));
    connect(okBtn,SIGNAL(clicked()),dlg,SLOT(close()));
    connect(cancelBtn,SIGNAL(clicked()),dlg,SLOT(close()));

    dlg->setModal(true);    //阻塞父窗口
    dlg->show();`
3. Qt判断SQL server中某张表是否存在的方法

解决代码:

 selectSql ="select * from sys.tables where name='"+tableName+"'";
 query->exec(selectSql);

 if(query->next())
    cout<<"the table is exsist!"<<endl;
 else
    cout<<"Not exsist!"<<endl
4.Qt判断SQLite中某张表是否存在的方法

解决代码:

selectSql = QString("select count(*) from sqlite_master where type='table' and name='%1'").arg(tableName);
flag = query->exec(selectSql);
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值