Lee_Serena 2024-06-09 14:36 采纳率: 14.3%
浏览 1

这是mypushbutton的cpp及h,还有窗口的cpp,但是我的返回按钮不显示


#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include"qpushbutton.h"
#include"qstring.h"
#include"qpropertyanimation.h"

#include <QWidget>

class MyPushButton : public QPushButton
{
    Q_OBJECT
    QString normalImagePath;
    QString pressImagePath;
public:
//    explicit MyPushButton(QWidget *parent = nullptr);
    MyPushButton(QString normalImage,QString pressImage=" ");
    void Zoom1();
    void Zoom2();

signals:

public slots:
};

#endif // MYPUSHBUTTON_H

```c++
 
#include "chooselevelscene.h"
#include"qmenubar.h"
#include"qmenu.h"
#include"qaction.h"
#include"qpainter.h"
#include"qpixmap.h"
 
 
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    QMenuBar *menuBar=new QMenuBar(this);
    setMenuBar(menuBar);
    QMenu *startMenu=menuBar->addMenu("开始");
    QAction *quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //返回按钮设置
         MyPushButton * backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
         backBtn->setParent(this);
         backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
 
}
 void ChooseLevelScene::paintEvent(QPaintEvent *event)
 {
     //加载背景
     QPixmap pix;
     pix.load(":/res/PlayLevelSceneBg.png");
     QPainter painter(this);
     painter.drawPixmap(0,0,this->width(),this->height(),pix);
     //加载标题
     pix.load(":/res/Title.png");
     pix=pix.scaled(pix.width()*1.5,pix.height()*1.5);
     painter.drawPixmap(this->width()*0.5-pix.width()*0.5,50,pix.width(),pix.height(),pix);
 
 }
 
```c++
 
#include "mypushbutton.h"
#include"QPixmap"
#include"qpainter.h"
#include"qdebug.h"
#include"qeasingcurve.h"
 
 
//MyPushButton::MyPushButton(QWidget *parent) : QWidget(parent)
//{
 
//}
MyPushButton::MyPushButton(QString normalImg,QString pressImg)
{
    normalImagePath=normalImg;
    pressImagePath=pressImg;
    QPixmap pix;
    bool ret=pix.load(normalImagePath);
   // pix.scaled(QSize(pix.width()*5,pix.height()*5));
    ////////////////////缩放图片要返回一个值,重新的一个pix才行
    if(!ret)
        qDebug()<<"图片加载失败";
    //缩放图片
    pix=pix.scaled(QSize(pix.width()*1.5,pix.height()*1.5));
    this->setFixedSize(pix.width(),pix.height());
    this->setIcon(QIcon(pix));
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIconSize(QSize(pix.width(),pix.height()));
 
}
 
 

```

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-09 14:36
    关注

    引自免费微信小程序:皆我百晓生

    从你提供的代码来看,主要问题是MyPushButton的构造函数没有正确地初始化QPushButton的基本属性,以及在ChooseLevelScene中可能存在的布局和位置计算问题。让我们逐一解决这些问题。

    首先,确保MyPushButton的构造函数正确地继承了QPushButton的行为,并且在加载图像时进行了正确的尺寸调整。此外,在ChooseLevelScene中,你需要确保MyPushButtonmove方法正确地考虑了窗口的实际大小,而不是基于某个固定值。

    修改MyPushButton构造函数

    MyPushButton的构造函数中,确保加载的图像被适当地设置为按钮的图标,并且按钮的大小与图标匹配。同时,应确保QPushButton的默认行为被正确初始化。

    MyPushButton::MyPushButton(QString normalImg, QString pressImg)
        : QPushButton() // 明确调用基类构造函数
    {
        normalImagePath = normalImg;
        pressImagePath = pressImg.isEmpty() ? normalImg : pressImg;
    
        QPixmap pix;
        bool ret = pix.load(normalImagePath);
        if (!ret) {
            qDebug() << "图片加载失败";
            return;
        }
    
        // 缩放图片并设置按钮大小
        pix = pix.scaled(QSize(pix.width() * 1.5, pix.height() * 1.5));
        this->setFixedSize(pix.width(), pix.height());
    
        // 设置图标和样式
        this->setIcon(QIcon(pix));
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIconSize(QSize(pix.width(), pix.height()));
    
        // 连接信号槽处理按钮状态变化
        connect(this, &QPushButton::pressed, this, &MyPushButton::Zoom1);
        connect(this, &QPushButton::released, this, &MyPushButton::Zoom2);
    }
    

    修改ChooseLevelScene中的按钮位置计算

    ChooseLevelScene中,你使用了move方法来定位按钮。这可能不会总是有效,特别是在窗口大小改变时。使用相对位置和比例可能会更好。

    ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
    {
        // ... 其他代码保持不变 ...
    
        // 返回按钮设置
        MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
        backBtn->setParent(this);
    
        // 使用相对位置和比例
        int posX = (this->width() - backBtn->width()) * 0.9; // 调整比例以适应不同屏幕
        int posY = (this->height() - backBtn->height()) * 0.9;
        backBtn->move(posX, posY);
    }
    

    确保重绘事件正确触发

    ChooseLevelScenepaintEvent中,你已经正确地绘制了背景和标题。然而,确保按钮在窗口重绘时仍然可见也很重要。通常情况下,Qt会自动处理这一点,但如果你遇到问题,可以检查是否在任何地方意外地覆盖了按钮的位置。

    通过以上修改,你应该能看到MyPushButton正确显示在ChooseLevelScene中。如果还有其他问题,请进一步描述,以便提供更具体的帮助。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月9日