QT Creator -C++ - Qlabel在MainWindow上未显示图像

发布于 2025-01-22 13:43:39 字数 8036 浏览 0 评论 0原文

我目前正在恢复一个旧项目,并且无法像以前那样在Qlabel上显示图像。这是一个小型旅行咨询应用程序,它使用Dijkstra的算法来计算从棒球体育场到另一个的最短/最具成本效益的路径,图像应在屏幕的右下方显示,并在图像顶部从体育场的顶部绘制一条线但是,当按下显示跳闸按钮时,要进行体育场和总里程,但图像没有显示。

这是我的MAINDOWN.CPP的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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


void MainWindow::on_pushButton_clicked()
{
    //get the path for the image
    QString fullpath = "MLB_Stadium_Map.png";

    QPixmap pixmap(fullpath);


    //get the dimensions and scaling of the image
    ui->label->setPixmap(fullpath);

    ui->label->setScaledContents( true );

    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPainter painter(&pixmap);

    //set pen and pen color
    QPen red ((QColor(255,0,0)),1);

    painter.setPen(red);

    //From here down it creates the shortest path to the current


    int final_distance = 0;

    vector<string> final_path;
    int initial_index;
    int final_index;


    //get from combo box what are the source and destination stadiums

    string ini_text = ui->comboBox_2->currentText().toStdString();
    string end_text = ui->comboBox->currentText().toStdString();


    if(ui->radioButton_2->isChecked())
    {
        ini_text = ui->comboBox_3->currentText().toStdString();
        end_text = ui->comboBox_4->currentText().toStdString();
    }
    else if(ui->radioButton_3->isChecked())
    {
        ini_text = ui->comboBox_5->currentText().toStdString();
        end_text = ui->comboBox_6->currentText().toStdString();
    }




    //convert the stadiums into index
    if(ini_text != end_text)
    {

        for(unsigned i = 0; i < MLB_S; i++)
        {
            if(MLB_stadiums.at(i).find(ini_text) != string::npos)
            {
                initial_index = i;
            }
        }

        for(unsigned i = 0; i < MLB_S; i++)
        {
            if(MLB_stadiums.at(i).find(end_text) != string::npos)
            {
                final_index = i;
            }
        }




        Dijkstra_MLB d(MLB_graph, initial_index, MLB_stadiums, final_index);



        final_distance = d.dijkstra(final_path);

        int x1 = 0;
        int y1 = 0;
        int x2 = 0;
        int y2 = 0;

        for(unsigned i = 0; i < final_path.size(); i++)
        {
            if(i + 1 >= final_path.size())
                break;

            for(int s = 0; s < MLB_S; s++)
            {
                if(cords_arr[s].getname().find(final_path.at(i)) != string::npos)
                {
                    x1 = cords_arr[s].getx();
                    y1 = cords_arr[s].gety();
                }
                else if(cords_arr[s].getname().find(final_path.at(i + 1)) != string::npos)
                {
                    x2 = cords_arr[s].getx();
                    y2 = cords_arr[s].gety();
                }
            }

            painter.drawLine(x1,y1,x2,y2);
        }

    }



    QString fd = QString::fromStdString(to_string(final_distance));
    ui->textBrowser->setText(fd);


    ui->label->setPixmap(pixmap);

}


void MainWindow::on_radioButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_radioButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_radioButton_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_custom_add_destination_clicked()
{
    if(custom_destinations.empty())
        custom_destinations.push_back(ui->comboBox_custom_source->currentText().toStdString());

    custom_destinations.push_back(ui->comboBox_custom_destinations->currentText().toStdString());
}

void MainWindow::on_pushButton_custom_display_trip_clicked()
{
    //get the path for the image
    QString fullpath = "MLB_Stadium_Map.png";

    QPixmap pixmap(fullpath);


    //get the dimensions and scaling of the image
    ui->label->setPixmap(fullpath);

    ui->label->setScaledContents( true );

    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPainter painter(&pixmap);

    //set pen and pen color
    QPen red ((QColor(255,0,0)),1);

    painter.setPen(red);

    //From here down it creates the shortest path to the current


    int final_distance = 0;

    vector<string> final_path;
    int initial_index;
    int final_index;


    //get from combo box what are the source and destination stadiums



    if(custom_destinations.size() >= 2)
    {

            int x1 = 0;
            int y1 = 0;
            int x2 = 0;
            int y2 = 0;

            for(unsigned z = 0; z < custom_destinations.size(); z++)
            {
                if((z + 1) >= custom_destinations.size())
                    break;

                string ini_text = custom_destinations.at(z);
                string end_text = custom_destinations.at(z + 1);




                //convert the stadiums into index
                if(ini_text != end_text)
                {

                    for(unsigned i = 0; i < MLB_S; i++)
                    {
                        if(MLB_stadiums.at(i).find(ini_text) != string::npos)
                        {
                            initial_index = i;
                        }
                    }

                    for(unsigned i = 0; i < MLB_S; i++)
                    {
                        if(MLB_stadiums.at(i).find(end_text) != string::npos)
                        {
                            final_index = i;
                        }
                    }


                Dijkstra_MLB d(MLB_graph, initial_index, MLB_stadiums, final_index);

                final_distance += d.dijkstra(final_path);



                for(unsigned i = 0; i < final_path.size(); i++)
                {
                    if(i + 1 >= final_path.size())
                        break;

                    for(int s = 0; s < MLB_S; s++)
                    {
                        if(cords_arr[s].getname().find(final_path.at(i))
                                != string::npos)
                        {
                            x1 = cords_arr[s].getx();
                            y1 = cords_arr[s].gety();
                        }
                        else if(cords_arr[s].getname().find(final_path.at(i + 1))
                                != string::npos)
                        {
                            x2 = cords_arr[s].getx();
                            y2 = cords_arr[s].gety();
                        }
                    }

                    painter.drawLine(x1,y1,x2,y2);
                }

                final_path.clear();

            }



        }

    }


    custom_destinations.clear();

    QString fd = QString::fromStdString(to_string(final_distance));
    ui->textBrowser_custom_total->setText(fd);


    ui->label->setPixmap(pixmap);


}



void MainWindow::on_pushButton_add_stadium_clicked()
{

    //get the path for the image
    QString fullpath = "MLB_Stadium_Map.png";

    QPixmap pixmap(fullpath);


    //get the dimensions and scaling of the image
    ui->label->setPixmap(fullpath);

    ui->label->setScaledContents( true );

    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPainter painter(&pixmap);

    //set pen and pen color
    QPen red ((QColor(255,0,0)),1);
    red.setWidth(6);

    painter.setPen(red);


    painter.drawPoint(ui->spinBox_xcord_add->value(), ui->spinBox_ycord_add->value());


    ui->label->setPixmap(pixmap);


    pixmap.save("MLB_Stadium_Map.png");

}

任何帮助都将受到高度赞赏。

I'm currently working on reviving an old project and can't get an image to display on the QLabel as it used to. This is a small trip advisory app that uses Dijkstra's Algorithm to calculate the shortest/most cost-efficient path from a baseball stadium to another, an image should display on the bottom right of the screen with a line drawn on top of the images from stadium to stadium and the total miles when the display trip button is pressed, however, the image is not showing.

enter image description here

Here is my code for maindown.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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


void MainWindow::on_pushButton_clicked()
{
    //get the path for the image
    QString fullpath = "MLB_Stadium_Map.png";

    QPixmap pixmap(fullpath);


    //get the dimensions and scaling of the image
    ui->label->setPixmap(fullpath);

    ui->label->setScaledContents( true );

    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPainter painter(&pixmap);

    //set pen and pen color
    QPen red ((QColor(255,0,0)),1);

    painter.setPen(red);

    //From here down it creates the shortest path to the current


    int final_distance = 0;

    vector<string> final_path;
    int initial_index;
    int final_index;


    //get from combo box what are the source and destination stadiums

    string ini_text = ui->comboBox_2->currentText().toStdString();
    string end_text = ui->comboBox->currentText().toStdString();


    if(ui->radioButton_2->isChecked())
    {
        ini_text = ui->comboBox_3->currentText().toStdString();
        end_text = ui->comboBox_4->currentText().toStdString();
    }
    else if(ui->radioButton_3->isChecked())
    {
        ini_text = ui->comboBox_5->currentText().toStdString();
        end_text = ui->comboBox_6->currentText().toStdString();
    }




    //convert the stadiums into index
    if(ini_text != end_text)
    {

        for(unsigned i = 0; i < MLB_S; i++)
        {
            if(MLB_stadiums.at(i).find(ini_text) != string::npos)
            {
                initial_index = i;
            }
        }

        for(unsigned i = 0; i < MLB_S; i++)
        {
            if(MLB_stadiums.at(i).find(end_text) != string::npos)
            {
                final_index = i;
            }
        }




        Dijkstra_MLB d(MLB_graph, initial_index, MLB_stadiums, final_index);



        final_distance = d.dijkstra(final_path);

        int x1 = 0;
        int y1 = 0;
        int x2 = 0;
        int y2 = 0;

        for(unsigned i = 0; i < final_path.size(); i++)
        {
            if(i + 1 >= final_path.size())
                break;

            for(int s = 0; s < MLB_S; s++)
            {
                if(cords_arr[s].getname().find(final_path.at(i)) != string::npos)
                {
                    x1 = cords_arr[s].getx();
                    y1 = cords_arr[s].gety();
                }
                else if(cords_arr[s].getname().find(final_path.at(i + 1)) != string::npos)
                {
                    x2 = cords_arr[s].getx();
                    y2 = cords_arr[s].gety();
                }
            }

            painter.drawLine(x1,y1,x2,y2);
        }

    }



    QString fd = QString::fromStdString(to_string(final_distance));
    ui->textBrowser->setText(fd);


    ui->label->setPixmap(pixmap);

}


void MainWindow::on_radioButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_radioButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_radioButton_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_custom_add_destination_clicked()
{
    if(custom_destinations.empty())
        custom_destinations.push_back(ui->comboBox_custom_source->currentText().toStdString());

    custom_destinations.push_back(ui->comboBox_custom_destinations->currentText().toStdString());
}

void MainWindow::on_pushButton_custom_display_trip_clicked()
{
    //get the path for the image
    QString fullpath = "MLB_Stadium_Map.png";

    QPixmap pixmap(fullpath);


    //get the dimensions and scaling of the image
    ui->label->setPixmap(fullpath);

    ui->label->setScaledContents( true );

    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPainter painter(&pixmap);

    //set pen and pen color
    QPen red ((QColor(255,0,0)),1);

    painter.setPen(red);

    //From here down it creates the shortest path to the current


    int final_distance = 0;

    vector<string> final_path;
    int initial_index;
    int final_index;


    //get from combo box what are the source and destination stadiums



    if(custom_destinations.size() >= 2)
    {

            int x1 = 0;
            int y1 = 0;
            int x2 = 0;
            int y2 = 0;

            for(unsigned z = 0; z < custom_destinations.size(); z++)
            {
                if((z + 1) >= custom_destinations.size())
                    break;

                string ini_text = custom_destinations.at(z);
                string end_text = custom_destinations.at(z + 1);




                //convert the stadiums into index
                if(ini_text != end_text)
                {

                    for(unsigned i = 0; i < MLB_S; i++)
                    {
                        if(MLB_stadiums.at(i).find(ini_text) != string::npos)
                        {
                            initial_index = i;
                        }
                    }

                    for(unsigned i = 0; i < MLB_S; i++)
                    {
                        if(MLB_stadiums.at(i).find(end_text) != string::npos)
                        {
                            final_index = i;
                        }
                    }


                Dijkstra_MLB d(MLB_graph, initial_index, MLB_stadiums, final_index);

                final_distance += d.dijkstra(final_path);



                for(unsigned i = 0; i < final_path.size(); i++)
                {
                    if(i + 1 >= final_path.size())
                        break;

                    for(int s = 0; s < MLB_S; s++)
                    {
                        if(cords_arr[s].getname().find(final_path.at(i))
                                != string::npos)
                        {
                            x1 = cords_arr[s].getx();
                            y1 = cords_arr[s].gety();
                        }
                        else if(cords_arr[s].getname().find(final_path.at(i + 1))
                                != string::npos)
                        {
                            x2 = cords_arr[s].getx();
                            y2 = cords_arr[s].gety();
                        }
                    }

                    painter.drawLine(x1,y1,x2,y2);
                }

                final_path.clear();

            }



        }

    }


    custom_destinations.clear();

    QString fd = QString::fromStdString(to_string(final_distance));
    ui->textBrowser_custom_total->setText(fd);


    ui->label->setPixmap(pixmap);


}



void MainWindow::on_pushButton_add_stadium_clicked()
{

    //get the path for the image
    QString fullpath = "MLB_Stadium_Map.png";

    QPixmap pixmap(fullpath);


    //get the dimensions and scaling of the image
    ui->label->setPixmap(fullpath);

    ui->label->setScaledContents( true );

    ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

    QPainter painter(&pixmap);

    //set pen and pen color
    QPen red ((QColor(255,0,0)),1);
    red.setWidth(6);

    painter.setPen(red);


    painter.drawPoint(ui->spinBox_xcord_add->value(), ui->spinBox_ycord_add->value());


    ui->label->setPixmap(pixmap);


    pixmap.save("MLB_Stadium_Map.png");

}

Any help is highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文