简体   繁体   English

如何在状态栏中的活动 QMdiAreaSubwindow 中的 qwidget 中显示 QLabel?

[英]How to display QLabel inside qwidget from active QMdiAreaSubwindow in statusbar?

I have a app that uses QMdiArea.我有一个使用 QMdiArea 的应用程序。

I want the text in the statusbar to update when another QMdiAreaSubwindow becomes active.我希望状态栏中的文本在另一个 QMdiAreaSubwindow 变为活动状态时更新。

So the text in the statusbar should become the same as the Qlabel text inside the QWidget wich is been displayed inside the QMdiAreaSubwindow.所以状态栏中的文本应该与 QWidget 中的 Qlabel 文本相同,显示在 QMdiAreaSubwindow 中。

But i can't find a way to do this.但我找不到办法做到这一点。 Right now the statusbar only shows the text from latest created QMdiAreaSubwindow.现在状态栏只显示最新创建的 QMdiAreaSubwindow 中的文本。 But it won't update the text in the statusbar(With qlabel from the qwidget) when another QMdiAreaSubwindow is selected. But it won't update the text in the statusbar(With qlabel from the qwidget) when another QMdiAreaSubwindow is selected.

As you can see in the screenshot, the text in the statusbar keeps saying "test2" but i want it to change to "text" from the active QMdiAreaSubwindow.正如您在屏幕截图中看到的,状态栏中的文本一直在说“test2”,但我希望它从活动的 QMdiAreaSubwindow 更改为“文本”。

在此处输入图片说明

mainwindow.h:主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <newwindow.h>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow 
{
    Q_OBJECT

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

    void NewSubWindow(QString name);

    void createStatusBar(QString name);

private slots:

    void on_actionNew_triggered();

    void on_mdiArea_subWindowActivated(QMdiSubWindow *arg1);

private:
    Ui::MainWindow *ui;

    NewWindow *nDialog;
};

#endif // MAINWINDOW_H

mainwindow.cpp:主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mdisubwidget.h"
#include "newwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    nDialog = new NewWindow();
    connect(nDialog,&NewWindow::transmit,this,&MainWindow::NewSubWindow);
}

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

void MainWindow::NewSubWindow(QString name) {

    // new Widget to add to mdiareasubwindow
    mdisubwidget *mdiwidget = new mdisubwidget();
    mdiwidget->addName(name);
    mdiwidget->setWindowTitle(name);

    // Create new mdiAreaSubWindow
    ui->mdiArea->addSubWindow(mdiwidget);

    // Show mdiArea
    mdiwidget->show();
}

void MainWindow::on_actionNew_triggered()
{
    nDialog->show();
}

void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{
    mdisubwidget *mdiwidget = new mdisubwidget(arg1->widget());
    qDebug() << "name" << mdiwidget->returnName();
    createStatusBar(mdiwidget->returnName());
}

void MainWindow::createStatusBar(QString name)
{
    statusBar()->showMessage("chart = "+name);
}

mdisubwidget.h mdissubwidget.h

#ifndef MDISUBWIDGET_H
#define MDISUBWIDGET_H

#include <QWidget>

namespace Ui {
    class mdisubwidget;
}

class mdisubwidget : public QWidget
{
    Q_OBJECT

public:
    explicit mdisubwidget(QWidget *parent = nullptr);
    void addName(QString name);
    QString returnName();
    ~mdisubwidget();

private:
    Ui::mdisubwidget *ui;
};

#endif // MDISUBWIDGET_H

mdisubwidget.cpp mdissubwidget.cpp

#include "mdisubwidget.h"
#include "ui_mdisubwidget.h"
#include "mainwindow.h"

QString currentName;

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

void mdisubwidget::addName(QString name) {
    ui->label_2->setText(name);
    currentName = name;
}

QString mdisubwidget::returnName() {

    return currentName;
}

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

NewWindow.h:新窗口.h:

#ifndef NEWWINDOW_H
#define NEWWINDOW_H

#include <QWidget>

namespace Ui {
    class NewWindow;
}

class NewWindow : public QWidget
{
    Q_OBJECT

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

signals:
    void transmit(QString name);

private slots:
    void on_pushButton_clicked();

private:
    Ui::NewWindow *ui;
};

#endif // NEWWINDOW_H

NewWindow.cpp:新窗口.cpp:

#include "newwindow.h"
#include "ui_newwindow.h"
#include "mainwindow.h"

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

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

void NewWindow::on_pushButton_clicked()
{
    QString name = ui->lineEdit->text();
    emit transmit(name);
}

ok you're using Qt Designer to connect the signal of subWindowActivated to the slot of on_mdiArea_subWindowActivated of your MainWindow , double check with qDebug in your on_mdiArea_subWindowActivated function if the name of your selected sub window appears on the console as you tried to change your current mdi sub window so follow my code snippets to find your way:确定你使用Qt Designer的信号连接subWindowActivated到的插槽on_mdiArea_subWindowActivated你的MainWindow ,具有双重检查qDebugon_mdiArea_subWindowActivated当你试图改变你目前的MDI功能,如果您选择的子窗口的名称出现在控制台上子窗口所以按照我的代码片段找到你的方式:

connect(ui->mdiArea, &QMdiArea::subWindowActivated, this, &DesignerWindow::activeViewChanged);

activeViewChanged(): activeViewChanged():

void DesignerWindow::activeViewChanged(QMdiSubWindow *activeSubWindow)
{
    // checks if there is no active sub window defined or the number of subwindows
    // are zero then return
    if (!activeSubWindow)
        return;
    if (ui->mdiArea->subWindowList().count() == 0) {
        ui->itemsTree->clear();
        return;
    }

    // defines the current Midi, View and graphical Scene when current sub window changes
    currentMidi = reinterpret_cast<MidiWindow*>(activeSubWindow->widget());
    currentView = reinterpret_cast<HMIView*>(currentMidi->internalView());
    currentScene = reinterpret_cast<HMIScene*>(currentMidi->internalScene());

    ItemsToolBar::ItemType currentType = currentScene->itemType();
    itemsToolBar->selectItemType(currentType);

    // updates the widgets and labels in status bar related to current midi sub window
    updateScale(currentView->zoomFactor() * 100);
    updateSelected();
    updateItemsTree();
    updateRendererType();
}

for example for updating the label in the status bar that holds the zooming factor of the current mdiSubWindow I wrote the updateScale procedure as below:例如,为了更新保存当前mdiSubWindow缩放系数的状态栏中的标签,我编写了updateScale过程,如下所示:

void DesignerWindow::updateScale(double _scale)
{
    scale = static_cast<int>(_scale);
    scaleLbl->setText(QString("%1%").arg(scale));
}

and finally I've noticed that your creating a label in status bar every time that you try to update the text in it, please avoid such a procedure and create a QLabel object and add it to your status bar as a permanent widget like below:最后我注意到您每次尝试更新其中的文本时都会在状态栏中创建一个标签,请避免这样的过程并创建一个 QLabel 对象并将其作为永久小部件添加到状态栏中,如下所示:

scaleLbl = new QLabel(this);
scaleLbl->setFrameStyle(QFrame::Sunken | QFrame::Panel);
scaleLbl->setMinimumWidth(50);
statusBar()->addPermanentWidget(scaleLbl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM