Qt学习 第14节:搜索窗口

 QApplication: No such file or directory 完美解决方案_五月花-CSDN博客_qapplication因为和Qt4相比,Qt5的模块结构发生了变化。由于Qt5将大部分桌面部件移到了Qt Widgets模块中,即QApplication已经从原来的移动到了。在.pro文件中添加 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets。即可完美解决类似QApplication: No such file or directory 这种新旧版本兼容问题。https://blog.csdn.net/friendbkf/article/details/45440175

FindDialog.pro

######################################################################
# Automatically generated by qmake (3.1) Wed Dec 22 19:24:23 2021
######################################################################

QT       += core gui
QT += widgets
#如果不加上面这句,那么在头文件中所有的对象类的引入都要改为#include<QtWidgets/类名>

TEMPLATE = app
TARGET = FindDialog
INCLUDEPATH += .

# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
HEADERS += finddialog.h
SOURCES += finddialog.cpp main.cpp

finddialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include <QtWidgets/QDialog>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDialog : public QDialog
{
    Q_OBJECT

public:
    FindDialog(QWidget *parent = 0);

signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif

finddialog.cpp

#include "finddialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
FindDialog::FindDialog(QWidget* parent)
    :QDialog(parent)
{
    label = new QLabel(tr("Find &what:"));//&表示快捷键Alt+w
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);//设置成伙伴,就是一个窗口部件,当按下快捷键时能接受焦点
    //例如本代码中,客户按下Alt+w,本来激活的是label,但是焦点会落在lineEdit
    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));

    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);//把find按钮设置成对话框的默认按钮,当客户按下enter键时,默认执行本按钮
    findButton->setEnabled(false);//find按钮设置成启动禁用

    closeButton = new QPushButton(tr("Close"));

    //QObject是FindDialog的父对象之一,所以可以省略connect()函数前面的QObject::前缀
    connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString&)));
    connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

    QHBoxLayout* topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    QVBoxLayout* leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);

    QVBoxLayout* rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch();//添加弹簧addStretch()
    //继承自void QBoxLayout::addStretch(int stretch = 0)
    //水平布局就是水平弹簧,垂直布局就是垂直弹簧

    QHBoxLayout* mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
//布局管理器不是窗口部件,派生自QLayout,进一步派生自QObject
    //当子布局对象添加到父布局对象中时,子布局对象会重新定义自己的父对象
    setLayout(mainLayout);//主布局器添加到对话框中去时,它就会成为对话框的子对象
    //他所包含的所有子窗口部件都会重定义自己的父对象,从而变成对话框中的子对象
    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
    //QWidget:;sizeHint()函数可以返回一个窗口部件所“理想”的尺寸大小
}

void FindDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
    backwardCheckBox->isChecked()?  emit findPrevious(text,cs) : emit findNext(text,cs);
}


void FindDialog::enableFindButton(const QString& text)
{
    findButton->setEnabled(!text.isEmpty());
}

main.cpp

#include <QApplication>
#include "finddialog.h"

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);

    FindDialog* dialog = new FindDialog;
    dialog->show();

    return app.exec();
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值