__ever 2015-12-18 08:12 采纳率: 33.3%
浏览 1921

QT实现模糊查询时出现的BUG

小弟使用的数据库时sqlite,图片说明

  • 写回答

1条回答 默认 最新

  • __ever 2015-12-18 08:15
    关注

    上代码://学生查询课程模块
    student_chack_course::student_chack_course(QWidget *parent)
    :QDialog(parent)
    {
    LineEdit_1=new QLineEdit;
    Label_1=new QLabel(tr("请输入关键字:"));
    Label_1->setBuddy(LineEdit_1);
    button_1=new QPushButton;
    button_1->setText(tr("查询"));
    button_1->setDefault(true);
    button_1->setEnabled(false);
    button_2=new QPushButton;
    button_2->setText(tr("关闭"));
    button_3=new QPushButton;
    button_3->setText(tr("另存为"));

    model=new QSqlRelationalTableModel;
    model->setTable("course");
    model->setSort(0,Qt::AscendingOrder);
    model->setHeaderData(0,Qt::Horizontal,tr("课程号"));
    model->setHeaderData(1,Qt::Horizontal,tr("课程名称"));
    model->setHeaderData(2,Qt::Horizontal,tr("先驱课程"));
    model->setHeaderData(3,Qt::Horizontal,tr("学分"));
    model->setHeaderData(4,Qt::Horizontal,tr("上课地点"));
    model->setHeaderData(5,Qt::Horizontal,tr("上课时间"));
    model->setRelation(2,QSqlRelation("course","cno","cname"));
    model->select();
    
    
    
    view=new QTableView;
    view->setModel(model);
    view->setSelectionMode(QAbstractItemView::SingleSelection);
    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->resizeColumnsToContents();
    view->setItemDelegate(new QSqlRelationalDelegate(view));
    
    QHeaderView *header=view->horizontalHeader();
    header->setStretchLastSection(true);
    
    
    connect(button_1,SIGNAL(clicked()),this,SLOT(to_record()));
    connect(LineEdit_1,SIGNAL(textChanged(const QString &)),this,SLOT(enable_button(const QString &)));
    connect(button_2,SIGNAL(clicked()),this,SLOT(to_close()));
    
    
    QHBoxLayout *down_layout=new QHBoxLayout;
    down_layout->addWidget(Label_1);
    down_layout->addWidget(LineEdit_1);
    down_layout->addWidget(button_1);
    down_layout->addWidget(button_2);
    down_layout->addWidget(button_3);
    down_layout->addStretch();
    
    QVBoxLayout *mainlayout=new QVBoxLayout;
    mainlayout->addWidget(view);
    mainlayout->addLayout(down_layout);
    setLayout(mainlayout);
    this->setFixedSize(500,400);
    

    }
    void student_chack_course::enable_button(const QString &text)//有内容输入时,查询--按钮--被--激--活
    {
    button_1->setEnabled(!text.isEmpty());
    }

    void student_chack_course::to_close()//关闭按钮实现
    {
    this->close();
    }

    void student_chack_course::to_record()//模糊匹配
    {
    model->setFilter(tr("cname='%1'").arg(QString("like %")+LineEdit_1->text()+QString("%")));
    model->select();

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误