标签类QLabel和行编辑框QLineEdit

1. 标签类:QLabel

用于提示作用,也可以显示视频、图像、文本等等

源码:

QLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())
QLabel(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())	

实例化和按钮差不多(可能没写全,参考即可)

    QLabel* label = new QLabel(this);
//    QLabel* label = new QLabel("标签",this);

1.1 代码实现标签的使用

1.1.1 标签的基本方法

label

mydialog.cpp

#include "mydialog.h"
#include "ui_mydialog.h"

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

    //设置标签文本
    this->label->setText("鲜肉");

    //设置标签大小
    this->label->resize(100,100);

    //移动标签位置
    this->label->move(100,0);

    //设置标签字体
//    this->label->setFont(QFont("黑体",15));

    //设置标签显示的数字(浮点型和整型)
    this->label->setNum(3.14);

    //获取标签显示的文本
    QString text = this->label->text();
    qDebug()<<"获取的内容数据为:"<<text<<endl;

    //设置标签中的内容缩进
//    this->label->setIndent(5);

    //设置文本对齐方式
    this->label->setAlignment(Qt::AlignLeft);//文本对其方式为右对齐
    //获取文本对齐方式
    Qt::Alignment flag= this->label->alignment();

    //设置标签换行
    this->label->setText("abcde fghijklmno prstuvwxyz");
    this->label->setWordWrap(true); //true 换行 false 不换

    //设置标签显示图片
    this->label->setPixmap(QPixmap(":/img/photo.jpg"));

    //设置标签自适应大小
    this->label->setScaledContents(true);//true 自适应  false 不自适应

    //设置文本格式
    this->label->setTextFormat(Qt::AutoText);

    //设置边距
    this->label->setMargin(1);//上下左右边距

    //设置gif()图
    QMovie* movie = new QMovie(":/img/photo2.gif");
    this->label->setMovie(movie);
    movie->start();//启动播放动画
    movie->setSpeed(600); //加速
    //停止,暂停
//    movie->stop();
//    movie->setPaused(true);

}

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


1.1.2 练习使用gif模拟视频播放器

能控制开始播放

能控制停止播放

能调节播放的速度

label_gif_test1

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QDebug>
#include <QMovie>
#include <QLabel>

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::Dialog *ui;
    QLabel* label1 = new QLabel(this);
    QLabel* label2 = new QLabel(this);

    //设置gif()图
    QMovie* movie = new QMovie(":/img/photo2.gif");
    int speed = 110;//初始速度

};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

// 练习使用gif模拟视频播放器
//能控制开始播放
//能控制停止播放
//能调节播放的速度
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->label1->setText("视频播放器");
    this->label1->move(100,100);

    this->label2->resize(700,400);
    this->label2->move(100,120);


    this->label2->setMovie(movie);
    this->movie->setSpeed(this->speed);
    movie->start();//开始播放

}

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


void Dialog::on_pushButton_clicked()//开始
{
    movie->start();//开始播放
}


void Dialog::on_pushButton_2_clicked()//暂停
{
    movie->setPaused(true);//暂停
}


void Dialog::on_pushButton_3_clicked()//加速
{

    static int count = 1;

    if(count ==0){
        movie->setSpeed(this->speed);//恢复速度
        qDebug()<<"当前速度为:"<<110<<endl;
        count++;
    }else if(count==1){
        movie->setSpeed(300);
        qDebug()<<"当前速度为:"<<300<<endl;
        count++;
    }else if(count==2){
        movie->setSpeed(600);
        qDebug()<<"当前速度为:"<<600<<endl;
        count=0;
    }

}

1.2 ui实现

各个选项解释

   rameShape:设置标签的边框样式
	frameShadow:设置边框阴影
	lineWidth:设置边框的宽度
	midLineWidth:设置线的宽度
	text:设置文本
	textFormat:设置文本格式
	pixMap:设置标签中的图像
	sclaedContents:设置自适应
	alignment:设置对齐方式
	wordWrap:设置是否换行
	margin:设置文本内容据边框的边距
	indent:设置缩进
	openExternLinks:放置外部链接

1.3 信号

当用户单击链接时发出该信号。锚引用的URL在link中传递

当用户将鼠标悬停在链接上时发出该信号。锚引用的URL在link中传递

2. 行编辑框:QLineEdit

实例化

    //实例化
    QLineEdit* lineEdit = new QLineEdit(this);
//    QLineEdit* lineEdit = new QLineEdit("请输入账号:",this);

2.1 代码实现

2.1.1 行编辑框的基本函数

设置行编辑框的文本可见模式的参数

QLineEdit::Normal    //正常

QLineEdit::NoEcho    //输入时不可见

QLineEdit::Password   //输入时密码模式

QLineEdit::PasswordEchoOnEdit  //输入时正常,输入完不可见

qline_edit

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

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

    //设置行编辑框大小
    this->lineEdit->resize(120,30);

    //设置行编辑框的位置
    this->lineEdit->move(80,80);

    //设置行编辑框背景的提示文本
    this->lineEdit->setPlaceholderText("请输入账号");

    //设置行编辑框最大输入的字符数
    this->lineEdit->setMaxLength(5);

    //设置行编辑框的文本可见模式
    this->lineEdit->setEchoMode(QLineEdit::Normal);//正常模式
//    this->lineEdit->setEchoMode(QLineEdit::NoEcho);//QLineEdit::NoEcho输入时看不见
//    this->lineEdit->setEchoMode(QLineEdit::Password);//密码模式

    //获取可见模式
    QLineEdit::EchoMode mode = this->lineEdit->echoMode();
    if(mode==QLineEdit::Normal){
        qDebug()<<"正常模式"<<endl;
    }

    //设置文本提示内容
//    this->lineEdit->setText("123");
    //获取行编辑框里的内容
    QString text = this->lineEdit->text();
    qDebug()<<text<<endl;

    //设置行编辑框是否只读
//    this->lineEdit->setReadOnly(true);//true 只读 false不只读

    //获取行编辑框最大输入数
    int len = this->lineEdit->maxLength();

}

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

2.1.2 练习:模拟登录界面,登录成功跳转计算机

模拟注册与登录界面

实现功能:

(1)实现注册/登录界面,登陆成功之后进入使用计算器界面(实现界面的跳转)

(2)用户能进行注册账户(支持注册多个账户)

(3)用户能对输入的密码进行隐藏或显示

QLinEdit_Login_Register

mydialog.h

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QDebug>

namespace Ui {
class MyDialog;
}

class MyDialog : public QDialog
{
    Q_OBJECT

public:
    explicit MyDialog(QWidget *parent = nullptr);
    ~MyDialog();

     void calculate();


public slots:
     void print1();
     void print2();
     void print3();
     void print4();
     void print5();
     void print6();
     void print7();
     void print8();
     void print9();
     void print0();
     void print_jia();
     void print_jian();
     void print_cheng();
     void print_chu();
     void print_deng();

private:
    Ui::MyDialog *ui;

    QPushButton* btn1 = new QPushButton("1",this);
    QPushButton* btn2 = new QPushButton("2",this);
    QPushButton* btn3 = new QPushButton("3",this);
    QPushButton* btn4 = new QPushButton("4",this);
    QPushButton* btn5 = new QPushButton("5",this);
    QPushButton* btn6 = new QPushButton("6",this);
    QPushButton* btn7 = new QPushButton("7",this);
    QPushButton* btn8 = new QPushButton("8",this);
    QPushButton* btn9 = new QPushButton("9",this);
    QPushButton* btn0 = new QPushButton("0",this);
    QPushButton* btn_jia = new QPushButton("+",this);
    QPushButton* btn_jian = new QPushButton("-",this);
    QPushButton* btn_cheng = new QPushButton("*",this);
    QPushButton* btn_chu = new QPushButton("/",this);
    QPushButton* btn_deng = new QPushButton("=",this);

    QString str1;//存储操作符之前的数字
    QString caozuofu = "";//存储操作符
    QString str2;//存储操作符之后的数字
};

#endif // MYDIALOG_H

mydialog.cpp

#include "mydialog.h"
#include "ui_mydialog.h"

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

    btn1->move(100,80);//移动位置
    btn2->move(200,80);//移动位置
    btn3->move(300,80);//移动位置

    btn4->move(100,120);//移动位置
    btn5->move(200,120);//移动位置
    btn6->move(300,120);//移动位置

    btn7->move(100,160);//移动位置
    btn8->move(200,160);//移动位置
    btn9->move(300,160);//移动位置

    btn0->move(200,200);//移动位置

    btn_jia->move(400,200);
    btn_jian->move(400,160);
    btn_cheng->move(400,120);
    btn_chu->move(400,80);

    btn_deng->move(300,200);

    //绑定按钮信号和本类的槽
    connect(this->btn1,SIGNAL(clicked()),this,SLOT(print1()));
    connect(this->btn2,SIGNAL(clicked()),this,SLOT(print2()));
    connect(this->btn3,SIGNAL(clicked()),this,SLOT(print3()));

    connect(this->btn4,SIGNAL(clicked()),this,SLOT(print4()));
    connect(this->btn5,SIGNAL(clicked()),this,SLOT(print5()));
    connect(this->btn6,SIGNAL(clicked()),this,SLOT(print6()));

    connect(this->btn7,SIGNAL(clicked()),this,SLOT(print7()));
    connect(this->btn8,SIGNAL(clicked()),this,SLOT(print8()));
    connect(this->btn9,SIGNAL(clicked()),this,SLOT(print9()));

    connect(this->btn0,SIGNAL(clicked()),this,SLOT(print0()));

    connect(this->btn_jia,SIGNAL(clicked()),this,SLOT(print_jia()));
    connect(this->btn_jian,SIGNAL(clicked()),this,SLOT(print_jian()));
    connect(this->btn_cheng,SIGNAL(clicked()),this,SLOT(print_cheng()));
    connect(this->btn_chu,SIGNAL(clicked()),this,SLOT(print_chu()));

    connect(this->btn_deng,SIGNAL(clicked()),this,SLOT(print_deng()));

}

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

void MyDialog::calculate(){


    int shuzi1 = str1.toInt();//字符转数字

    int shuzi2 = str2.toInt();//字符转数字

    if(caozuofu.at(0)=="+"){
        shuzi1+shuzi2;
        qDebug()<<shuzi1<<" + "<<shuzi2<<" = "<<shuzi1+shuzi2<<endl;
    }else if(caozuofu.at(0)=="-"){
        qDebug()<<shuzi1<<" - "<<shuzi2<<" = "<<shuzi1-shuzi2<<endl;
    }else if(caozuofu.at(0)=="*"){
        qDebug()<<shuzi1<<" * "<<shuzi2<<" = "<<shuzi1*shuzi2<<endl;
    }else if(caozuofu.at(0)=="/"){
        qDebug()<<shuzi1<<" / "<<shuzi2<<" = "<<(float)shuzi1/shuzi2<<endl;
    }

    str1.clear();//清除字符串
    str2.clear();
    caozuofu.clear();

}

void MyDialog::print1(){

    //获取按钮文本
    QString text1 = btn1->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text1);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text1);
        qDebug()<<str2;
    }
}

void MyDialog::print2(){

    //获取按钮文本
    QString text2 = btn2->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text2);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text2);
        qDebug()<<str2;
    }
}
void MyDialog::print3(){

    //获取按钮文本
    QString text3 = btn3->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text3);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text3);
        qDebug()<<str2;
    }
}
void MyDialog::print4(){

    //获取按钮文本
    QString text4 = btn4->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text4);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text4);
        qDebug()<<str2;
    }
}
void MyDialog::print5(){

    //获取按钮文本
    QString text5 = btn5->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text5);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text5);
        qDebug()<<str2;
    }
}
void MyDialog::print6(){

    //获取按钮文本
    QString text6 = btn6->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text6);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text6);
        qDebug()<<str2;
    }
}
void MyDialog::print7(){

    //获取按钮文本
    QString text7 = btn7->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text7);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text7);
        qDebug()<<str2;
    }
}
void MyDialog::print8(){

    //获取按钮文本
    QString text8 = btn8->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text8);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text8);
        qDebug()<<str2;
    }
}
void MyDialog::print9(){

    //获取按钮文本
    QString text9 = btn9->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text9);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text9);
        qDebug()<<str2;
    }
}
void MyDialog::print0(){

    //获取按钮文本
    QString text0 = btn0->text();

    //但这里用户输入的肯定不是操作符
    if(caozuofu.isEmpty()){//判断是存储操作符之前的数字还是之后的数字
        //用户还没输入操作符,所以数字应该记录在操作符之前的字符串里
        str1.append(text0);
        qDebug()<<str1;
    }else{
        //存入操作符之后的字符串里
        str2.append(text0);
        qDebug()<<str2;
    }
}

void MyDialog::print_jia(){
    //获取按钮文本
    QString text_jia = btn_jia->text();

    //这个if的作用方式用户多次点击操作符
    if(caozuofu.isEmpty()){//操作符得空进入
        caozuofu.append(text_jia);
        qDebug()<<caozuofu;
    }
}
void MyDialog::print_jian(){

    //获取按钮文本
    QString text_jian = btn_jian->text();

    if(caozuofu.isEmpty()){//操作符得空进入
        caozuofu.append(text_jian);
        qDebug()<<caozuofu;
    }
}
void MyDialog::print_cheng(){

    //获取按钮文本
    QString text_cheng = btn_cheng->text();

    if(caozuofu.isEmpty()){//操作符得空进入
        caozuofu.append(text_cheng);
        qDebug()<<caozuofu;
    }
}
void MyDialog::print_chu(){

    //获取按钮文本
    QString text_chu = btn_chu->text();

    if(caozuofu.isEmpty()){//操作符得空进入
        caozuofu.append(text_chu);
        qDebug()<<caozuofu;
    }
}

void MyDialog::print_deng(){

    if(!str1.isEmpty() && !caozuofu.isEmpty() && !str2.isEmpty()){
        this->calculate();//直接跳转计算函数
    }else{
        qDebug()<<"请确保输入正确,请重新输入"<<endl;
        str1.clear();//清除字符串
        str2.clear();
        caozuofu.clear();
    }

}

界面设计

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QDebug>
#include <mydialog.h>

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    void on_pushButton_clicked();//账号

    void on_pushButton_2_clicked();//密码

    void set_Password_State();//密码隐藏

private:
    Ui::Dialog *ui;
    QStringList list;//存放注册的账户链表
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

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

    //绑定密码隐藏信号
    connect(ui->radioButton,SIGNAL(clicked()),this,SLOT(set_Password_State()));

}

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

//将密码隐藏
void Dialog::set_Password_State()
{
    static int i=0;
    i++;
    if(i%2==1)//奇数
    {
        //将密码隐藏
        ui->lineEdit_2->setEchoMode(QLineEdit::Password);//隐藏密码
    }
    if(i%2==0)//偶数
    {
        //将密码显示
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);//正常显示密码
    }
}

//登录
void Dialog::on_pushButton_clicked()
{
    //获取用户账号密码
    QString user=ui->lineEdit->text();
    QString password=ui->lineEdit_2->text();

    //这里做登录校验
    //从字符串链表中取出账号和密码
    for(int i=0;i<list.size();i+=2)//遍历
    {

        if(this->list[i]==user)//循环比较账号
        {
            //账号正确以后,再来校验密码
            if(this->list[i+1]==password)//比较password字符串
            {
                //登录成功
                qDebug()<<"登录成功,欢迎使用!"<<endl;

                //打开另外一个界面(计算器)
                this->close();//关闭当前界面

                MyDialog m;
                m.setWindowTitle("计算器");
                m.exec();//以模态打开(阻塞模式打开)
            }
            else//密码错误
            {
                qDebug()<<"密码输入错误,登录失败!"<<endl;

                ui->lineEdit->clear();
                ui->lineEdit_2->clear();
                return;
            }
        }

    }

    //到这里说明账号不对
    qDebug()<<"账号输入错误,登录失败!"<<endl;
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();

}

//注册
void Dialog::on_pushButton_2_clicked()
{

    QString user=ui->lineEdit->text();//获取行编辑框中的账号
    QString password=ui->lineEdit_2->text();//获取行编辑框中的密码


    //存入 字符串链表
    list<<user<<password;
    for(int i=0;i<list.size();i+=2)//遍历
    {
        qDebug()<<"账号:"<<list[i]<<endl;
        qDebug()<<"密码:"<<list[i+1]<<endl;
    }

    //存入字符串后清除行编辑框中的内容便于下次输入
    ui->lineEdit->clear();
    ui->lineEdit_2->clear();
}


先注册

登录成功

4.2 信号

4.2.1 信号函数

void cursorPositionChanged(int oldPos, int newPos)  //当光标位置,改变时触发该信号,将光标新的位置和就的位置进行返回
 void editingFinished()  //编辑完成时触发该信号
 void inputRejected()  //输入被拒绝时触发信号
 void returnPressed()  //返回按钮按下时信号
 void selectionChanged() //文本选择变化时触发该信号
 void textChanged(const QString &text) //文本变化时触发该信号
 void textEdited(const QString &text) //文本编辑时触发该信号

4.2.2 信号的触发示例

qline_edit

绑定

dialog.cpp

//绑定光标变化触发信号
    connect(this->lineEdit,SIGNAL(cursorPositionChanged(int,int)),this,SLOT(receicve_Cursor_Change(int,int)));
    //绑定文本变化触发信号
    connect(this->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(receive_Text_Change(QString)));

定义

dialog.h

public slots:
    void receicve_Cursor_Change(int oldPos,int newPos);//光标发生改变
    void receive_Text_Change(const QString& text);//文本发生改变

实现

dialog.cpp

//光标改变触发
void Dialog::receicve_Cursor_Change(int oldPos, int newPos){
    qDebug()<<"旧的位置"<<oldPos<<endl;
    qDebug()<<"新的位置"<<newPos<<endl;
}

//文本发生改变
void Dialog::receive_Text_Change(const QString &text){
    qDebug()<<"文本:"<<text<<endl;
}

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用QLabel和QPushButton来实现这个功能。首先,创建一个QLabel对象,将其设置为可拖拽,并将其放置在窗口。然后,创建一个QPushButton对象,并将其放置在QLabel。最后,使用QLineEdit对象来实时更改按钮的位置。 以下是示例代码: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit class DragButton(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) self.setAcceptDrops(True) def mouseMoveEvent(self, e): if e.buttons() != Qt.LeftButton: return mimeData = QMimeData() drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft()) dropAction = drag.exec_(Qt.MoveAction) def mousePressEvent(self, e): super().mousePressEvent(e) if e.button() == Qt.LeftButton: self.dragStartPos = e.pos() def dragEnterEvent(self, e): e.accept() def dropEvent(self, e): self.move(e.pos() - self.dragStartPos) e.setDropAction(Qt.MoveAction) e.accept() class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 280, 170) self.setWindowTitle('Drag Button') lbl = QLabel('Button', self) lbl.move(100, 65) lbl.setAcceptDrops(True) button = DragButton('Drag me', self) button.move(100, 100) edit = QLineEdit('', self) edit.move(100, 135) edit.textChanged.connect(lambda: button.move(int(edit.text()), button.y())) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在这个示例,我们创建了一个名为DragButton的自定义QPushButton,它允许拖拽。我们还创建了一个QLabel对象,将其设置为可拖拽,并将其放置在窗口。然后,我们创建了一个QPushButton对象,并将其放置在QLabel。最后,我们使用QLineEdit对象来实时更改按钮的位置。 请注意,这个示例的代码并不完整,你需要自己添加一些必要的引用和变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值