Lee_Serena 2024-06-09 14:31 采纳率: 14.3%
浏览 18

qt中设置返回按钮,但是不显示


#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:31
    关注

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

    从你提供的代码来看,主要问题是MyPushButton子类化了QWidget而不是QPushButton。这导致了虽然你创建了按钮,但它没有被识别为一个标准的按钮控件,因此可能不会显示出来。要解决这个问题,你需要确保MyPushButton继承自QPushButton

    下面是一个修正后的MyPushButton类的实现示例,它应该能正确地显示你的返回按钮:

    #include <QPushButton>
    #include <QPixmap>
    #include <QPainter>
    #include <QDebug>
    
    class MyPushButton : public QPushButton {
    public:
        MyPushButton(QString normalImg, QString pressImg) : QPushButton("", nullptr) {
            normalImagePath = normalImg;
            pressImagePath = pressImg;
    
            QPixmap pix;
            bool ret = pix.load(normalImagePath);
            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()));
        }
    
    protected:
        void paintEvent(QPaintEvent *event) override {
            QPushButton::paintEvent(event);
            QPainter painter(this);
            if (this->underMouse()) {
                QPixmap pix;
                bool ret = pix.load(pressImagePath);
                if (ret) {
                    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
                }
            } else {
                QPixmap pix;
                bool ret = pix.load(normalImagePath);
                if (ret) {
                    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
                }
            }
        }
    
    private:
        QString normalImagePath;
        QString pressImagePath;
    };
    

    在上述代码中:

    • MyPushButton现在继承自QPushButton
    • 在构造函数中,使用QPushButton("", nullptr)初始化,确保它是作为一个按钮创建的。
    • paintEvent方法中,根据鼠标是否悬停在按钮上,加载并绘制不同的图像。

    确保你在ChooseLevelScene类中使用这个修改后的MyPushButton类,并且正确地调用了show()方法以显示窗口。如果你没有在main()函数中调用exec()来启动事件循环,记得加上这一行代码:

    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
        ChooseLevelScene w;
        w.show();
        return a.exec();
    }
    

    这样应该可以解决按钮不显示的问题。如果还有其他疑问或遇到新的问题,请随时告诉我。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月9日