小鸟游菜花 2024-05-16 21:39 采纳率: 86.5%
浏览 38

为什么自定义的窗口加入到stackwidget后运行没有显示


#include "enterleave.h"
#include<QVBoxLayout>
#include<QLabel>

enterleave::enterleave(QWidget *parent)
    : QWidget{parent}
{
    //垂直布局
    QVBoxLayout *vlayout=new QVBoxLayout(this);
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);

    QLabel *label=new QLabel(this);
    label->setText("鼠标进入/离开");
    label->setFrameShape(QFrame::Box);
    //设置样式表
    label->setStyleSheet("background-color:blue;font-size:25px");
    //设置固定高度
    label->setFixedHeight(50);
    //设置居中
    //setAlignment用于设置文本、控件或布局的对齐方式
    label->setAlignment(Qt::AlignCenter);

    //设置垂直布局
    vlayout->addWidget(label);
}

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPushButton>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include"contextevent.h"
#include"dragevent.h"
#include"keyevent.h"
#include"painteevent.h"
#include"pressmove.h"
#include"propagate.h"
#include"timer.h"
#include"enterleave.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 左侧导航窗口是垂直布局
    QVBoxLayout *layout = new QVBoxLayout(ui->leftwidget);
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);


    createbotton();
    initmain();

}

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

void MainWindow::createbotton()
{

    QStringList names;
    names<<"鼠标进入/离开"
          <<"鼠标按下/移动/释放"
          <<"键盘事件"
          <<"定时器事件"
          <<"拖动事件"
          <<"绘图事件"
          <<"右键菜单"
          <<"总结:事件的传递流程";

    btngroup=new QButtonGroup(this);
    ui->leftwidget->setStyleSheet(R"(
        QPushButton {
            font: 14px;
            text-align : left;
        }
    )");
    for(int i=0;i<names.count();i++)
    {

        QPushButton *btn =new QPushButton;

        //设置固定高度
        btn->setFixedSize(190,51);
        //设置文字
        btn->setText("  "+QString("%1.%2").arg(i+1,2,10,QChar('0')).arg(names.at(i)));

        //设置按钮可选中,类似于复选框的功能
        btn->setCheckable(true);

        //将按钮添加到按钮组,并设置索引
        btngroup->addButton(btn,i);

        // 将按钮添加到布局中
        ui->leftwidget->layout()->addWidget(btn);
    }
    //设置默认第一个按钮选中
    btngroup->button(0)->setChecked(true);
}

void MainWindow::initmain()
{
    //int addWidget(QWidget *w);
    ui->stackedWidget->addWidget(new enterleave());
    ui->stackedWidget->addWidget(new pressmove());
    ui->stackedWidget->addWidget(new keyevent());
    ui->stackedWidget->addWidget(new timer());
    ui->stackedWidget->addWidget(new dragevent());
    ui->stackedWidget->addWidget(new painteevent());
    ui->stackedWidget->addWidget(new contextevent());
    ui->stackedWidget->addWidget(new propagate());

}


img

img

  • 写回答

2条回答 默认 最新

  • 阿里嘎多学长 2024-05-16 21:39
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    您想要解决的问题

    您提问的目的是解决在Qt框架下自定义窗口加入到QStackedWidget后,运行时没有显示的问题。

    图片内容

    1. 第一张图片展示了一个事件处理流程的概览,包括鼠标事件、键盘事件、定时器事件、拖动事件、绘图事件、右键菜单和事件传递流程的总结。
    2. 第二张图片是一个Qt Designer生成的UI布局图,展示了MainWindow窗口中包含的组件,如centralwidgetmainwidgetleftwidget以及stackedWidget

    问题出现原因

    根据您提供的代码和图片,问题可能出现的原因有:

    • 自定义窗口可能没有正确初始化或设置,导致在QStackedWidget中不可见。
    • QStackedWidget的当前索引可能没有设置为新添加的自定义窗口的索引,因此默认显示的是第一个窗口。
    • 如果QStackedWidget或其子部件的可见性(setVisible)被设置为false,则不会显示。

    问题解决方案

    为了解决这个问题,您可以尝试以下步骤:

    1. 确保自定义窗口在添加到QStackedWidget后被正确初始化,并且其可见性设置为true
    2. 在添加自定义窗口到QStackedWidget后,设置当前索引为新添加的窗口的索引。
    3. 检查QStackedWidget及其子部件的可见性设置。

    提供代码

    以下是修改后的initmain函数,其中包含了设置当前索引的步骤:

    void MainWindow::initmain()
    {
        // ... 其他代码 ...
    
        // 添加自定义窗口到QStackedWidget
        int enterleaveIndex = ui->stackedWidget->addWidget(new enterleave());
        int pressmoveIndex = ui->stackedWidget->addWidget(new pressmove());
        // ... 添加其他自定义窗口 ...
    
        // 设置当前索引为新添加的自定义窗口的索引
        // 假设我们想显示enterleave窗口
        ui->stackedWidget->setCurrentIndex(enterleaveIndex);
    
        // ... 其他代码 ...
    }
    

    代码运行方式

    • 确保您的开发环境已经安装了Qt框架。
    • 将上述代码集成到您的Qt项目中。
    • 在Qt Creator中运行项目。

    代码预期运行结果

    如果代码正确运行,您应该会看到enterleave窗口显示在QStackedWidget中,且具有蓝色背景和相应的文本。

    推荐相关链接

    以上链接可以帮助您更深入地了解QStackedWidget的使用和Qt的布局管理。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月16日

悬赏问题

  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题