itfunc 2022-09-25 17:05 采纳率: 75%
浏览 43

C++ QT类库的入门学习

入门学习qt
有已经从事此行业的朋友讲讲是一边查文档一边写么,
我贴一点我学习过程中的代码,给我的感觉就是东西太多了,很难记.
感谢赐教

#include "widget.h"
#include <QPushButton>
#include <QDebug>
#include <QMenu>
#include <QToolButton>
#include <QStyle>
#include <QRadioButton>
#include <QLabel>
#include <QGroupBox>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QDate>
#include <QIntValidator>
#include <QDoubleValidator>
#include <QLabel>
#include <QGridLayout>
#include <QMovie>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    resize(640, 480);
    //    ToolButton();
    //    PushButton();
    //    RadioButton();
    //    CheckBox();
//    LineEdit();
//    LineAction();
//    Label();
    LabelShowImage();
}

Widget::~Widget()
{
}

void Widget::PushButton()
{
    resize(640, 480);
    auto* btn = new QPushButton("open", this);
    btn->setText("文件");

    connect(btn, &QPushButton::clicked, this, [](bool chick)
    {
        qDebug() << "clicked" << chick;
    });

    connect(btn, &QPushButton::pressed, this, []()
    {
        qDebug() << "pressed";
    });

    connect(btn, &QPushButton::released, this, []()
    {
        qDebug() << "released";
    });

    //按钮必须能被选择才会触发toggled信号
    btn->setCheckable(true);
    btn->setChecked(true);  //设置选中状态
    connect(btn, &QPushButton::toggled, this, [](bool chick)
    {
        qDebug() << "toggled" << chick;
    });
}

void Widget::ToolButton()
{
    auto* toolBtn = new QToolButton(this);
    toolBtn->setText("ToolButton");
    toolBtn->setIcon(QIcon("://images/loginIcon.png"));
    //系统自带的Icon
    toolBtn->setIcon(style()->standardIcon(QStyle::StandardPixmap::SP_ArrowBack));
    toolBtn->setFixedSize(100, 100);
    //toolBtn->setIconSize(QSize(100, 100));
    toolBtn->setIconSize(toolBtn->size() / 2);

    //设置一下文本的位置
    toolBtn->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextUnderIcon);

    toolBtn->setArrowType(Qt::ArrowType::UpArrow);
}

void Widget::RadioButton()
{
    //单选按钮
    auto* label = new QLabel("choose your favorite animal", this);
    auto* r_cat_btn = new QRadioButton("cat", this);
    auto* r_dog_btn = new QRadioButton("dog", this);

    r_cat_btn->move(0, 30);
    r_dog_btn->move(0, 60);

    connect(r_cat_btn, &QRadioButton::toggled, this, [](bool chick)
    {
        qDebug() << "chick" << chick;
    });

    auto group_box = new QGroupBox(this);
    auto lab = new QLabel("Which of the following is true?", group_box);
    auto btn1 = new QRadioButton("kpsun it the most handsome", group_box);
    auto btn2 = new QRadioButton("kpsun is the most uglily", group_box);
    auto btn3 = new QRadioButton("you are handsome", group_box);

    group_box->move(200, 0);
    group_box->setFixedSize(300, 300);

    lab->move(0, 0);
    btn1->move(0, 30);
    btn2->move(0, 60);
    btn3->move(0, 90);
}

void Widget::CheckBox()
{
    //一组选择,会放在一个组框里面
    auto* group = new QGroupBox("What sports do you like?", this);
    group->setFixedSize(200, 200);
    //往框里添加复选按钮
    auto* cb = new QCheckBox("basketball", group);
    auto* cb1 = new QCheckBox("basketball2", group);
    auto* cb2 = new QCheckBox("basketball3", group);
    auto* cb3 = new QCheckBox("basketball4", group);

    //使用布局
    auto vlayout = new QVBoxLayout(group);
    vlayout->addWidget(cb);
    vlayout->addWidget(cb1);
    vlayout->addWidget(cb2);
    vlayout->addWidget(cb3);
}

void Widget::LineEdit()
{
    auto lineEdit = new QLineEdit(this);

    //光标移动的时候就可以获取文本
    connect(lineEdit, &QLineEdit::cursorPositionChanged, this, [=]()
    {
        //qDebug() << "cursorPositionChanged:" << lineEdit->text();
    });

    //编辑完成信号,按回车显示输出文字
    connect(lineEdit, &QLineEdit::editingFinished, this, [=]()
    {
        //qDebug() << "cursorPositionChanged:" << lineEdit->text();
    });

    //输入拒绝
    connect(lineEdit, &QLineEdit::inputRejected, this, [=]()
    {
        //qDebug() << "cursorPositionChanged:" << lineEdit->text();
    });

    //回车出发,和回车一样的
    connect(lineEdit, &QLineEdit::returnPressed, this, [=]()
    {
        //qDebug() << "returnPressed:" << lineEdit->text();
    });

    //选中文本触发
    connect(lineEdit, &QLineEdit::selectionChanged, this, [=]()
    {
        //qDebug() << "returnPressed:" << lineEdit->text();
    });

    //文本改变
    connect(lineEdit, &QLineEdit::textChanged, this, []()
    {
        //qDebug() << "textChanged";
    });

    //文本编辑,和文本改变一样的
    connect(lineEdit, &QLineEdit::textEdited, this, []()
    {
        qDebug() << "textEdited";
    });

    lineEdit->setText("Hello");

    //设置密码模式
    //NoEcho,输入过程中不显示文本信息
    //Normal,输入什么就显示什么
    //Password, 圆黑点
    lineEdit->setEchoMode(QLineEdit::EchoMode::PasswordEchoOnEdit);

    auto edit = new QLineEdit(this);
    edit->move(0, 30);

    //设置掩码
    //edit->setText(QDate::currentDate().toString("yyyy-MM-dd"));
    //edit->setInputMask("9999-99-99");

    //只能用大写的
    //edit->setInputMask(">AAAA-AAAA-AAAA");

    //设置验证器
    //范围输入
    //edit->setValidator(new QIntValidator(0, 100, edit));

//    connect(edit, &QLineEdit::textEdited, this, [=]()
//    {
//        qDebug() << edit->text();
//    });
    //小数点后3位小数
    edit->setValidator(new QDoubleValidator(0.0, 99.9, 3, edit));
}

void Widget::LineAction()
{
    auto edit = new QLineEdit(this);
    //添加动作
    auto userAct = edit->addAction(QIcon(":/images/user.png"),QLineEdit::ActionPosition::LeadingPosition);
    auto delAct = edit->addAction(QIcon(":/images/delete.png"), QLineEdit::ActionPosition::TrailingPosition);

    connect(delAct, &QAction::triggered, edit, &QLineEdit::clear);
}

void Widget::Label()
{
    //QLabel标签,用来显示文本或者图片
    auto lab = new QLabel("我是l3213213123123123123123able", this);
    lab->setFixedWidth(200);
    lab->setStyleSheet("background-color:green; color:white");
    //设置对齐方式,都是枚举类型
    lab->setAlignment(Qt::AlignmentFlag::AlignRight);

    //当文本超过固定的宽度之后,自动换行
    lab->setWordWrap(true);

    //设置超链接
    auto laba = new QLabel("<a href=\"https://goskp.github.io/\">访问我的小站</a>", this);
    laba->move(0, 30);

    //自己手动处理
    connect(laba, &QLabel::linkActivated, this, []()
    {
        //鼠标点击就显示
        //qDebug() << "快看啊,";
    });
    connect(laba, &QLabel::linkHovered, this, []()
    {
        //鼠标碰上就显示,仅一次
        //qDebug() << "快看啊,";
    });
    //设置自动打开浏览器
    laba->setOpenExternalLinks(true);

    //设置伙伴,给lable来个快捷键s
    auto nameLable = new QLabel("&name", this);
    auto nameEdit = new QLineEdit(this);

    auto userLable = new QLabel("用户(&U)", this);
    auto userEdit = new QLineEdit(this);

    //网格布局
    auto glayout = new QGridLayout(this);

    glayout->addWidget(nameLable, 0, 0);
    glayout->addWidget(nameEdit, 0, 1);
    glayout->addWidget(userLable, 1, 0);
    glayout->addWidget(userEdit, 1, 1);

    //把nameLabel设置为nameLabel的伙伴
    nameLable->setBuddy(nameEdit);
    userLable->setBuddy(userEdit);
}

void Widget::LabelShowImage()
{
    //显示图片
    auto lab = new QLabel(this);
    //设置图片
    lab->setPixmap(QPixmap(":/images/label_img.jpg"));
    //固定lable的大小之后,图片显示不完整
    lab->setFixedSize(250, 150);
    //图片自适应Label的大小
    lab->setScaledContents(true);

    //显示gif图
    auto labGif = new QLabel(this);
    /*labGif->setPixmap(QPixmap(":/images/label_gif.gif"));*/
    auto movie = new QMovie(":/images/label_gif.gif");
    labGif->setMovie(movie);
    movie->start();
}
#include "mainwindow.h"
#include <QMenuBar>
#include <QStyle>
#include <QDebug>
#include <QShortcut>
#include <QToolBar>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    resize(640, 480);
    InitMenuBar();
    InitToolBar();
    InitStatus();
}

MainWindow::~MainWindow()
{
}

void MainWindow::InitMenuBar()
{
    //获取提供的MenuBar
    //获取的本身在窗口上
    QMenuBar* bar = menuBar();
    //自己new一个MenuBar
    QMenuBar* selfBar = new QMenuBar;
    //this->setMenuBar(selfBar);

    //给菜单添加菜单项
    //给菜单添加快捷键,,在写菜单文本的时候用&符号表示
    QMenu* fileMenu = bar->addMenu("文件(&F)");
    QMenu* editMenu = bar->addMenu("编辑(&E)");

    //给selfMenuBar设置一下
    selfBar->addMenu("File");
    selfBar->addMenu("Edit");

    this->setMenuBar(bar);

    //添加动作到菜单上
    auto newFileAct = fileMenu->addAction("新建文件", this, []()
    {
        qDebug() << "3,newFileAct";
    });
    auto openFileAct = fileMenu->addAction(style()->standardIcon(QStyle::StandardPixmap::SP_FileLinkIcon),"打开文件", this, []()
    {
        qDebug() << "3,openFileAct";
    });
    auto saveAct = new QAction(style()->standardIcon(QStyle::StandardPixmap::SP_DirIcon),"保存文件");
    fileMenu->addAction(saveAct);

/*
    //1.直接连接动作的信号,这种方法写着太伤身体
//    connect(newFileAct, &QAction::triggered, this, []()
//    {
//        qDebug() << "newFileAct";
//    });
//    connect(openFileAct, &QAction::triggered, this, []()
//    {
//        qDebug() << "openFileAct";
//    });
//    connect(saveAct, &QAction::triggered, this, []()
//    {
//        qDebug() << "saveAct";
//    });

*/
    //2.通过menu的信号处理动作的信号
    connect(fileMenu, &QMenu::triggered, this, [=](QAction* act)
    {
        if (act == newFileAct) {
            qDebug() << "trigged" << act->text();
        } else if (act == openFileAct) {
            qDebug() << "trigged" << act->text();
        } else if (act == saveAct) {
            qDebug() << "tirgged" << act->text();
        }
    });

    //3.直接在添加动作的时候添加处理函数
    //    auto newFileAct = fileMenu->addAction("新建文件", this, [](){ qDebug() << "3,newFileAct"; });

    //给动作添加快捷键
    newFileAct->setShortcut(QKeySequence("Ctrl+n"));
    openFileAct->setShortcut(QKeySequence("ctrl+o"));

    //设置动作可以选中
    newFileAct->setCheckable(true);
}

void MainWindow::InitToolBar()
{
    //添加工具栏
    QToolBar* toolBar = addToolBar("toolBar");
    //添加动作,图片最好通过资源文件添加,不要用绝对路径
    toolBar->addAction(QIcon("./images/zay.png"), "动作");
    toolBar->addAction("hello");
    toolBar->addWidget(new QPushButton("hhh"));
 
    QToolBar* toolBar1 = addToolBar("toolBar");
    //添加动作,图片最好通过资源文件添加,不要用绝对路径
    toolBar1->addAction(QIcon("./images/zay.png"), "动作");
    toolBar1->addAction("hello");
    toolBar1->addWidget(new QPushButton("hhh"));
}

void MainWindow::InitStatus()
{
    //获取自带的状态栏
    QStatusBar* bar = statusBar();
    bar->addWidget(new QLabel("kpsun"));
    bar->addWidget(new QPushButton(""));
    
}
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-25 18:02
    关注
    评论

报告相同问题?

问题事件

  • 修改了问题 9月25日
  • 创建了问题 9月25日

悬赏问题

  • ¥15 kotlin multiplaform 的共享模块支持蓝牙吗还是说安卓和ios的蓝牙都要自己配
  • ¥15 为什么我的查询总是显示无数据,该怎么修改呢(语言-java)
  • ¥15 IDDPM采样结果异常
  • ¥100 求一个,王者荣耀查询隐藏战绩的教材,爬虫代码
  • ¥60 Cocos creator缓动问题
  • ¥15 专业问题提问,7月5号2点之前
  • ¥25 使用cube ai 导入onnx模型时报错
  • ¥15 关于#微信小程序#的问题:用一个网页显示所有关联的微信小程序数据,包括每个小程序的用户访问量
  • ¥15 root的安卓12系统上,如何使apk获得root或者高级别的系统权限?
  • ¥20 关于#matlab#的问题:如果用MATLAB函数delayseq可以对分数延时,但是延时后波形较原波形有幅度上的改变