c++ qt 主窗口设置图像后的对话框"确定"按钮

c++ qt Dialog 'OK' button after Mainwindow set image

本文关键字:对话框 确定 按钮 图像 qt 窗口 设置 c++      更新时间:2023-10-16

我想要'ok'的新表单

From Mainwindow label Set image
image path : ./org/org.jpg

addim.h

private slots:
void on_buttonBox_accepted();  <---- 'OK' button

addim.cpp

void AddIm::on_ADdimg_cliekd()
system("raspistill -w 480 -h 360 -q 20 -o aaa.jpg -t 2");
QPixmap pix("./org/org.jpg);      
ui2->QWidjet_org->setPixmap(pix);  <--- QWidjet_org this label

我设法执行了有效的最小代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    //This will change the image in the main window
    void buttonHere();
    //This will change the image in the dialog box
    void buttonDialog();
private:
    Ui::MainWindow *ui;
    Dialog dialog;
    //We need to save the image in QImage so it doesn't disappear
    QImage image;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->pushButton_here, SIGNAL(clicked(bool)), SLOT(buttonHere()));
    QObject::connect(ui->pushButton_dialog, SIGNAL(clicked(bool)), SLOT(buttonDialog()));
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::buttonHere()
{
    //We load the image
    image.load(ui->label->text());
    //We set the label image
    ui->label_2->setPixmap(QPixmap::fromImage(image));
}
void MainWindow::buttonDialog()
{
    //We call the function that changes the dialog image
    dialog.setImage(ui->label->text());
    //We show the dialog
    dialog.show();
}

对话框

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    //This function changes the image
    void setImage(QString path);
private:
    Ui::Dialog *ui;
    //We need to save the image in QImage so it doesn't disappear
    QImage image;
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::setImage(QString path)
{
    //We load the image
    image.load(path);
    //We set the label image
    ui->label->setPixmap(QPixmap::fromImage(image));
}

你得到这个结果

https://i.stack.imgur.com/gdqd7.png

https://i.stack.imgur.com/kbrsa.png

您应该创建一个资源文件,在其中导入图片,然后使用以下示例代码:

QPixmap pixmap_btn_hw(":/new/myPrefix/btn_hardware.png");
QIcon ButtonIcon_hw(pixmap_btn_hw);
ui->hardware_btn->setIcon(ButtonIcon_hw);
ui->hardware_btn->setIconSize(pixmap_btn_hw.rect().size());
ui->hardware_btn->setFocusPolicy(Qt::NoFocus);

启动外部程序" sytem()"是一个坏主意。以下要好得多:

QProcess *prpr = new QProcess();
prpr->setProcessChannelMode(QProcess::MergedChannels);
prpr->start("raspistill -w 480 -h 360 -q 20 -o aaa.jpg -t 2");
if (!prpr->waitForFinished())
{
   // there was an error
}
else
{
   // command worked!
}