QT6 基础编程一(001-1)之在窗口的一个点上点击时弹出一个不同的子窗口3秒后消失,此例程也可以用于实现很多种不同窗口的弹出,包括定点显示子窗口的程序,不过需要稍加修改。
cmakelist.txt
cmake_minimum_required(VERSION 3.5)
project(label_mouse VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
set(PROJECT_SOURCES
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(label_mouse
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET label_mouse APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(label_mouse SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(label_mouse
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(label_mouse PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
set_target_properties(label_mouse PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(label_mouse)
endif()
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QDialog *dialog;
bool event(QEvent *e)override;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<QMouseEvent>
#include<QDialog>
#include<iostream>
#include <unistd.h>
using namespace std;
//QDialog *dialog = new QDialog; //QWidget: Must construct a QApplication before a QWidget
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
QEvent *e;
ui->setupUi(this);
// this->event(e); // 此处会导致程序崩溃,不用添加
update();
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::event(QEvent *event){
static bool press=false;
static QPoint PreDot;
//QTimer time;
if(event->type() == QEvent::MouseButtonPress )
{
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
//判断鼠标是否是左键按下,且鼠标位置是否在绘画区域
if(mouse->button()==Qt::LeftButton &&ui->label->geometry().contains(mouse->pos()))
//if(ui->label->geometry().contains(mouse->pos())) //左右弹窗同时可以
{
press=true;
QApplication::setOverrideCursor(Qt::OpenHandCursor); //设置鼠标样式
PreDot = mouse->pos();
QDialog *dialog = new QDialog;
//QDialog *dialog2;
dialog->resize(120,120);
dialog->show();
sleep(1);
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
//if(mouse->button()==Qt::LeftButton && ui->label->geometry().contains(mouse->pos())){
if(mouse->button()==Qt::LeftButton && ui->label->geometry().contains(mouse->pos())){
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
PreDot = mouse->pos();
cout<<PreDot.x()<<PreDot.y()<<endl;
//sleep(1);
}
sleep(1);
//QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
QApplication::setOverrideCursor(Qt::ArrowCursor); //改回鼠标样式
dialog->hide();
//press=false;
}
}
//else if(event->type() == QEvent::MouseButtonRelease)
else if(0)
{
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
//判断鼠标是否是左键释放,且之前是在绘画区域
if(mouse->button()==Qt::LeftButton && press )
{
QApplication::setOverrideCursor(Qt::ArrowCursor); //改回鼠标样式
press=false;
}
}
return true;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
实现效果如下图所示,点击textlabel即可实现弹窗: