如何在QT中添加状态栏不使用表单并显示文本的长度及其坐标正在刷新?

问题描述

我想将状态栏添加到我的程序中,但我在网上找不到任何示例。

我看到很少有人使用我不使用的某些表格。我希望我的状态栏能够显示文本长度和他的坐标。而且我想在每次更改后刷新它(更改文本及其位置)。

我的代码目前可以更改显示文本、使用鼠标或键盘更改其位置并将该数据保存到文件中或打开该文件并加载数据。

我的代码

#include <QApplication>
#include <QLabel>
#include <QMainWindow>
#include <QMouseEvent>
#include <QMessageBox>
#include <QVBoxLayout>
#include <qformlayout>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QPainter>
#include <QFileDialog>
#include <QTextStream>
#include <QStatusBar>
#include <QProgressBar>
#include <QTextEdit>

class MyDialog : public QDialog {
public:
    MyDialog();
    QVBoxLayout* mainLayout;
    QWidget* editWidget;
    qformlayout* editLayout;
    QLineEdit* lineEdit;
    QDialogButtonBox* buttonBox;

};

MyDialog::MyDialog() {
    lineEdit = new QLineEdit;
    editLayout = new qformlayout;
    editLayout->addRow(new QLabel(tr("Novi tekst:")),lineEdit);
    editWidget = new QWidget;
    editWidget->setLayout(editLayout);
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok |QDialogButtonBox::Cancel);
    connect(buttonBox,SIGNAL(accepted()),this,SLOT(accept()));
    connect(buttonBox,SIGNAL(rejected()),SLOT(reject()));
    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(editWidget);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);
    setwindowTitle("Promjena teksta");

}


class MyMainWindow : public QMainWindow {
public:
    MyMainWindow();
    QLabel* MyLabel;
    QMenu* EditMenu;
    QAction* EditNoviTekst;
    QString* String;
    QString*poz;
    int a,b;


    void mousepressEvent(QMouseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void paintEvent(QPaintEvent *event) override;

    void EditNoviTekstMenu();


    void FileSaveAsMenu();
    void FileOpenMenu();
    QMenu* FileMenu;
    QAction* FileSaveAs;
    QAction* FileOpen;
    QStatusBar* StatusBar;

};

void MyMainWindow::mousepressEvent(QMouseEvent* event) {
    if (event->button() == Qt::LeftButton) {
        MyLabel->move(event->x(),event->y());
        a=MyLabel->x();
        b=MyLabel->y();
    }
}

void MyMainWindow::keyPressEvent(QKeyEvent *event) {
    switch( event->key() ){
        case Qt::Key_Left: MyLabel->move(MyLabel->pos().x()-1,MyLabel->pos().y());
        a=MyLabel->x();
        b=MyLabel->y();
            break;
        case Qt::Key_Right: MyLabel->move(MyLabel->pos().x()+1,MyLabel->pos().y());
        a=MyLabel->x();
        b=MyLabel->y();
            break;
        case Qt::Key_Up: MyLabel->move(MyLabel->pos().x(),MyLabel->pos().y()-1);
        a=MyLabel->x();
        b=MyLabel->y();
            break;
        case Qt::Key_Down: MyLabel->move(MyLabel->pos().x(),MyLabel->pos().y()+1);
        a=MyLabel->x();
        b=MyLabel->y();
        break;
}
}


MyMainWindow::MyMainWindow() {
    MyLabel = new QLabel(this);
    MyLabel->setText("Hello World!");
    MyLabel->move(10,20);
    QString xxx;
    a=MyLabel->x();
    b=MyLabel->y();
     //(*String)=MyLabel->text();

     xxx=MyLabel->text();
    //int duzina=xxx.length();
    String=&xxx;
    int duzina=(*String).length();

    FileSaveAs = new QAction(tr("&Save As..."),this);
    FileSaveAs->setShortcut(tr("CTRL+S"));
    connect(FileSaveAs,&QAction::triggered,&MyMainWindow::FileSaveAsMenu);

    FileOpen = new QAction(tr("&Open..."),this);
    FileOpen->setShortcut(tr("CTRL+O"));
    connect(FileOpen,&MyMainWindow::FileOpenMenu);

    FileMenu = menuBar()->addMenu(tr("&File"));
    FileMenu->addAction(FileSaveAs);
    FileMenu->addAction(FileOpen);

    EditNoviTekst = new QAction(tr("&Novi tekst..."),this);
    EditNoviTekst->setShortcut(tr("CTRL+N"));
    connect(EditNoviTekst,&MyMainWindow::EditNoviTekstMenu);

    EditMenu = menuBar()->addMenu(tr("&Edit"));
    EditMenu->addAction(EditNoviTekst);



    QStatusBar *statusBar = this->statusBar();


    QLabel *coord = new QLabel( tr("  Pozicija  ") );
    QLabel *length = new QLabel( tr(" Duzina  ") );




    coord->setMinimumSize( coord->sizeHint() );
    coord->setAlignment( Qt::AlignCenter );
    //coord->setText(QString::number(MyLabel->x())+","+QString::number(MyLabel->y()));
    coord->setText(QString::number(a)+","+QString::number(b));
    coord->setToolTip( tr("Koordinate teksta."));

    statusBar->addPermanentWidget( coord );

    length->setMinimumSize( length->sizeHint() );
    length->setAlignment( Qt::AlignCenter );


    length->setText(QString::number(duzina));

    length->setToolTip( tr("Govori nam duzinu teksta..."));

    statusBar->addWidget( length );
    statusBar->showMessage( tr("Ready"),500 );

}
void MyMainWindow::FileSaveAsMenu(){
    QString fileName = QFileDialog::getSaveFileName(this,"Save As...","","FESB File (*.fsb)");

        if (!fileName.isEmpty()) {
            QFile file(fileName);
            if (!file.open(qiodevice::writeonly)) {
                QMessageBox::information(this,"Unable to Open File",file.errorString());
                return;
            }
            QTextStream out(&file);
            out << "fesb file" << endl;
            out << MyLabel->text() << endl;
            out << MyLabel->pos().x() << endl;
            out << MyLabel->pos().y() << endl;
            out << pos().x() << endl;
            out << pos().y() << endl;
            out << size().width() << endl;
            out << size().height() << endl;
        }
}
void MyMainWindow::FileOpenMenu(){
    QString fileName = QFileDialog::getopenFileName(this,"Open Geometry...","FESB File (*.fsb)");
    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(qiodevice::ReadOnly)) {
            QMessageBox::information(this,file.errorString());
            return;
        }
    QTextStream in(&file);
    QString str; str = in.readLine();
        if(str=="fesb file") {
            str = in.readLine();
            MyLabel->setText(str);
            int x,y,w,h;
            in >> x >> y;
            MyLabel->move(x,y);
            in >> x >> y >> w >> h;
            this->setGeometry(x,h);
        }
    }
}


void MyMainWindow::EditNoviTekstMenu() {
MyDialog dialog;
int ret = dialog.exec();
if( ret == QDialog::Accepted ) {
MyLabel->setText(dialog.lineEdit->text());
//String->append(dialog.lineEdit->text());
(*String)=dialog.lineEdit->text();
}
}

void MyMainWindow::paintEvent(QPaintEvent*){
QSize size = this->size();
QPainter painter(this);
painter.drawLine(0,size.height(),size.width(),20);
}






int main(int argc,char **argv) {
    QApplication app (argc,argv);
    MyMainWindow mainWindow;
    mainWindow.resize(300,150);
    mainWindow.show();



    return app.exec();
}

解决方法

从 qmainwindow: 以下是 MyMainWindow 类的最小示例:

void MyMainWindow::updateStatusBar(){
    QString statusBar_info = QString("Len:%1,x:%2,y:%2")
    .arg(MyLabel->fontMetrics().boundingRect(MyLabel->text()).width()) //text_len
    .arg(MyLabel->x())                                               //text_x
    .arg(MyLabel->y());                                              //text_y
    statusBar()->showMessage(statusBar_info);
}
,

我从你发布的另一个问题中得到了答案,现在已删除:

要解决这个问题,您必须将 coord 和 length 变量设为全局变量,以便可以在其他函数中访问和修改它们,这是更新后的代码:

#include <QApplication>
#include <QLabel>
#include <QMainWindow>
#include <QMouseEvent>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QPainter>
#include <QFileDialog>
#include <QTextStream>
#include <QStatusBar>
#include <QProgressBar>
#include <QTextEdit>

class MyDialog : public QDialog {
public:
    MyDialog();
    QVBoxLayout* mainLayout;
    QWidget* editWidget;
    QFormLayout* editLayout;
    QLineEdit* lineEdit;
    QDialogButtonBox* buttonBox;

};

MyDialog::MyDialog() {
    lineEdit = new QLineEdit;
    editLayout = new QFormLayout;
    editLayout->addRow(new QLabel(tr("Novi tekst:")),lineEdit);
    editWidget = new QWidget;
    editWidget->setLayout(editLayout);
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok |QDialogButtonBox::Cancel);
    connect(buttonBox,SIGNAL(accepted()),this,SLOT(accept()));
    connect(buttonBox,SIGNAL(rejected()),SLOT(reject()));
    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(editWidget);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);
    setWindowTitle("Promjena teksta");

}

class MyMainWindow : public QMainWindow {
public:
    MyMainWindow();
    QLabel* MyLabel;
    QMenu* EditMenu;
    QAction* EditNoviTekst;
    QLabel *coord;
    QLabel *length;
    QString* String;
    QString*poz;
    int a,b;


    void mousePressEvent(QMouseEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
    void paintEvent(QPaintEvent *event) override;

    void EditNoviTekstMenu();


    void FileSaveAsMenu();
    void FileOpenMenu();
    QMenu* FileMenu;
    QAction* FileSaveAs;
    QAction* FileOpen;
    QStatusBar* StatusBar;

};

void MyMainWindow::mousePressEvent(QMouseEvent* event) {
    if (event->button() == Qt::LeftButton) {
        MyLabel->move(event->x(),event->y());
         a=MyLabel->pos().x();
         b=MyLabel->pos().y();
         coord->setText(QString::number(a)+","+QString::number(b));
    }
}

void MyMainWindow::keyPressEvent(QKeyEvent *event) {
    switch( event->key() ){
        case Qt::Key_Left: MyLabel->move(MyLabel->pos().x()-1,MyLabel->pos().y());
        a=MyLabel->pos().x()-1;
        b=MyLabel->pos().y();
        coord->setText(QString::number(a)+","+QString::number(b));
            break;
        case Qt::Key_Right: MyLabel->move(MyLabel->pos().x()+1,MyLabel->pos().y());
        a=(int)MyLabel->pos().x()+1;
        b=MyLabel->pos().y();
        coord->setText(QString::number(a)+","+QString::number(b));
            break;
        case Qt::Key_Up: MyLabel->move(MyLabel->pos().x(),MyLabel->pos().y()-1);
        a=MyLabel->pos().x();
        b=MyLabel->pos().y();
        coord->setText(QString::number(a)+","+QString::number(b));
            break;
        case Qt::Key_Down: MyLabel->move(MyLabel->pos().x(),MyLabel->pos().y()+1);
        a=MyLabel->pos().x();
        b=MyLabel->pos().y()+1;
        coord->setText(QString::number(a)+","+QString::number(b));
        break;
}
}


MyMainWindow::MyMainWindow() {
    MyLabel = new QLabel(this);
    MyLabel->setText("Hello World!");
    MyLabel->move(10,20);
    QString xxx;
    a=MyLabel->pos().x();
    b=MyLabel->pos().y();

     //(*String)=MyLabel->text();

     xxx=MyLabel->text();
    //int duzina=xxx.length();
    String=&xxx;
    int duzina=(*String).length();


    FileSaveAs = new QAction(tr("&Save As..."),this);
    FileSaveAs->setShortcut(tr("CTRL+S"));
    connect(FileSaveAs,&QAction::triggered,&MyMainWindow::FileSaveAsMenu);

    FileOpen = new QAction(tr("&Open..."),this);
    FileOpen->setShortcut(tr("CTRL+O"));
    connect(FileOpen,&MyMainWindow::FileOpenMenu);

    FileMenu = menuBar()->addMenu(tr("&File"));
    FileMenu->addAction(FileSaveAs);
    FileMenu->addAction(FileOpen);

    EditNoviTekst = new QAction(tr("&Novi tekst..."),this);
    EditNoviTekst->setShortcut(tr("CTRL+N"));
    connect(EditNoviTekst,&MyMainWindow::EditNoviTekstMenu);

    EditMenu = menuBar()->addMenu(tr("&Edit"));
    EditMenu->addAction(EditNoviTekst);



    QStatusBar *statusBar = this->statusBar();


    coord = new QLabel( tr("  Pozicija  ") );
    length = new QLabel( tr(" Duzina  ") );




    coord->setMinimumSize( coord->sizeHint() );
    coord->setAlignment( Qt::AlignCenter );
    //coord->setText(QString::number(MyLabel->x())+","+QString::number(MyLabel->y()));
    coord->setText(QString::number(a)+","+QString::number(b));
    coord->setToolTip( tr("Koordinate teksta."));

    statusBar->addPermanentWidget( coord );

    length->setMinimumSize( length->sizeHint() );
    length->setAlignment( Qt::AlignCenter );


    length->setText(QString::number(duzina));

    length->setToolTip( tr("Govori nam duzinu teksta..."));

    statusBar->addWidget( length );
    statusBar->showMessage( tr("Ready"),500 );

}
void MyMainWindow::FileSaveAsMenu(){
    QString fileName = QFileDialog::getSaveFileName(this,"Save As...","","Your File (*.ur)");

        if (!fileName.isEmpty()) {
            QFile file(fileName);
            if (!file.open(QIODevice::WriteOnly)) {
                QMessageBox::information(this,"Unable to Open File",file.errorString());
                return;
            }
            QTextStream out(&file);
            out << "fesb file" << endl;
            out << MyLabel->text() << endl;
            out << MyLabel->pos().x() << endl;
            out << MyLabel->pos().y() << endl;
            out << pos().x() << endl;
            out << pos().y() << endl;
            out << size().width() << endl;
            out << size().height() << endl;
        }
}
void MyMainWindow::FileOpenMenu(){
    QString fileName = QFileDialog::getOpenFileName(this,"Open Geometry...","FESB File (*.fsb)");
    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::information(this,file.errorString());
            return;
        }
    QTextStream in(&file);
    QString str; str = in.readLine();
        if(str=="fesb file") {
            str = in.readLine();
            MyLabel->setText(str);
            int x,y,w,h;
            in >> x >> y;
            MyLabel->move(x,y);
            in >> x >> y >> w >> h;
            this->setGeometry(x,h);
        }
    }
}


void MyMainWindow::EditNoviTekstMenu() {
MyDialog dialog;
int ret = dialog.exec();
if( ret == QDialog::Accepted ) {
MyLabel->setText(dialog.lineEdit->text());
length->setText(QString::number(MyLabel->text().length()));

//String->append(dialog.lineEdit->text());

}
}

void MyMainWindow::paintEvent(QPaintEvent*){
QSize size = this->size();
QPainter painter(this);
painter.drawLine(0,size.height(),size.width(),20);
}






int main(int argc,char **argv) {
    QApplication app (argc,argv);
    MyMainWindow mainWindow;
    mainWindow.resize(300,150);
    mainWindow.show();



    return app.exec();
}

同时还有一堆其他的变量不需要像 a 和 b 那样全局变量(一目了然),可能还有更多。这不会影响您的代码速度或其他任何东西,但会随着代码的增长而更难维护。