c ++ qt主窗口设置图像后的“确定”按钮

时间:2015-05-07 11:08:27

标签: c++ qt button

我希望在“确定”之后点击新表单

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

2 个答案:

答案 0 :(得分:0)

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

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);

启动外部程序,&#34; sytem()&#34;是个坏主意。以下是更好的:

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!
}

答案 1 :(得分:0)

我设法做了一个有效的最小代码:

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();
}

Dialog.h

#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));
}

你得到这个结果

http://i.imgur.com/sguOqLS.png

http://i.imgur.com/p2rCK9f.png