1,主界面设计
2,代码展示
i,mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"mylabel.h"
#include<QString>
class Mylabel;
class QLabel;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void clicked();
void doubleClicked(); // 双击信号
public slots:
void slotClicked();
void slotdoubleClicked();
public:
Mylabel *my_label;
//QLabel *label;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
ii,mylabel
#ifndef MYLABEL_H
#define MYLABEL_H
#include<QLabel>
#include<QString>
#include<QWidget>
#include<QMainWindow>
#include"QMessageBox"
class Mylabel:public QLabel
{
Q_OBJECT
public:
Mylabel(QWidget * parent = nullptr);
~Mylabel(){}
signals:
void clicked();
void doubleClicked(); // 双击信号
public slots:
void slotClicked();
void slotdoubleClicked();
protected:
void mousePressEvent(QMouseEvent* event);
void mouseDoubleClickEvent(QMouseEvent *event); // 鼠标双击事件
};
#endif // MYLABEL_H
iii,main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
iv,mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtWidgets>
#include"mylabel.h"
#include<QString>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// my_label=new Mylabel("label1",this);
ui->label_3->setStyleSheet("border:1px solid black");
connect(ui->label_3, SIGNAL(clicked()), this, SLOT(slotClicked()));
connect(ui->label_3, SIGNAL(doubleClicked()), this, SLOT(slotdoubleClicked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotClicked()
{
ui->label_3->setWindowFlags(Qt::Window);
ui->label_3->setScaledContents(true);
ui->label_3->showFullScreen();
//qDebug()<<"on_label_clicked";
}
void MainWindow::slotdoubleClicked()
{
ui->label_3->setWindowFlags(Qt::Window);
ui->label_3->setScaledContents(true);
ui->label_3->showNormal ();
}
v,mylabel
#include<QLabel>
#include"mylabel.h"
#include"mainwindow.h"
Mylabel::Mylabel(QWidget * parent)
:QLabel(parent)
{
// this->setText(text);
connect(this, SIGNAL(clicked()), this, SLOT(slotClicked()));
connect(this, SIGNAL(doubleClicked()), this, SLOT(slotdoubleClicked()));
}
void Mylabel::slotClicked()
{
// QMessageBox::information(nullptr, QString::fromLocal8Bit("单击"),
// QString::fromLocal8Bit("123"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
void Mylabel::slotdoubleClicked()
{
}
void Mylabel::mousePressEvent(QMouseEvent* event)
{
emit clicked();
}
void Mylabel::mouseDoubleClickEvent(QMouseEvent* event)
{
emit doubleClicked();
}