当前位置:网站首页>Six simple cases of QT

Six simple cases of QT

2022-07-05 09:56:00 Sunqk5665

One 、 Login screen

The base class is Dialog, Add a as the main interface MainWindow class
Directory structure :
 Insert picture description here

pro Add the following code to the file to add the icon :

RC_ICONS = myico.ico

Landing interface design :
 Insert picture description here
dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include "QMessageBox"//

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    
    delete ui;
}

void Dialog::on_pushButton_2_clicked()//
{
    
    close();
}

void Dialog::on_pushButton_clicked()//
{
    
    if(ui->lineEdit->text().trimmed()=="abc123"&&ui->lineEdit_2->text().trimmed()=="123"){
    
        accept();
    }
    else {
    
        QMessageBox::question(this,tr(" Warning "),tr(" Wrong user name or password !!!"),QMessageBox::Yes,QMessageBox::No);
    }
}

main.cpp

#include "dialog.h"
#include <QApplication>
#include"mainwindow.h"

int main(int argc, char *argv[])
{
    
    QApplication a(argc, argv);
    Dialog w;
    MainWindow w1;//
    if(w.exec()==QDialog::Accepted){
    //
        w1.show();//
    }//

    return a.exec();
}

mainwindow.cpp Don't change

Two 、 Menus and toolbars

Create a new one MainWindow, Double click to add menu name and submenu name , Carriage return application , Add English half angle character ==(&F)== You can use shortcut keys at runtime alt+F Trigger .
 Insert picture description here
Add Icon of submenu :
Double click on the action
 Insert picture description here
 Insert picture description here
Of course, this is just a simple addition to the menu , Not endowed with function , You can also add some functions , For example, you can add a text copy function , Here you can go to my other article Qt stay MainWndow Realize the text copy function in the menu bar in
Be careful : Don't add image resources directly to the project path , It can be placed outside the upper level directory or the upper level directory , or ,, Hey ,, You can't load it ...

3、 ... and 、 Keyboard events

turn ~:https://blog.csdn.net/weixin_43624626/article/details/115493337

Four 、 Mouse events ( Roller …)

turn ~:https://blog.csdn.net/weixin_43624626/article/details/115493337

5、 ... and 、 Event filtering ( Mouse button )

turn ~https://blog.csdn.net/weixin_43624626/article/details/115700771

6、 ... and 、 Set irregular form ( transparency )

1、 Create a new one Qt Widget application , Base class selection QWidget, The class name can remain the default .
2、 Add a background transparent png picture
 Insert picture description here
3、 stay widget.cpp Add header file to
 Insert picture description here

#include<QPixmap>
#include<QBitmap>
#include<QPainter>

4、 Get into widget.h In the source file , Declare two event handlers
 Insert picture description here

protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *);

5、 Then add the following code to the constructor , Used to load pictures 、 Set the window size and call setMask() Function to set the shield for the window .
 Insert picture description here

QPixmap pix;
pix.load("../buguize/touming.png");
resize(pix.size());
setMask(pix.mask());

6、 Add two more event handlers , A drawing for pictures , The other one is used to click the image with the mouse to close the window .
 Insert picture description here

void Widget::paintEvent(QPaintEvent *){
    
    QPainter painter(this);
    painter.drawPixmap(0,0,QPixmap("../buguize/touming.png"));

}
void Widget::mousePressEvent(QMouseEvent *){
    
    close();
}

7、 Build and run it to see the effect
 Insert picture description here

原网站

版权声明
本文为[Sunqk5665]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140535206265.html