开发者

Setting the pixmap of a QLabel in QT framework

My problem is mainly around two UI files, Form.ui and Form1.ui:

in Form i have multiple QLabels (a png pic is set by default , because we have lots of pics we can only show a small portion of them) and a button for each QLabel widget. once this button is clicked , form1 should appear with a large QLabel showing the pic the use clicked on largely... void Form:: ButtonPic1() should handle the event of the click , and it will show an instance of Form1 and it should set the Qlabel pic of Form1 to test.png

Edited:

so this is the form.cpp

#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"

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


}

Form::~Form()
{
    delete ui;
}
void Form:: ButtonPic1()
{
    QPixmap  mypix  (":/karim/test.png");



       Form1* f= new Form1();
         f->setLabelPixmap(mypix);
         f->showFullScreen();
   开发者_运维技巧      f->raise();
         f->activateWindow();
         this->close();

}

This is Form1.cpp

#include "form1.h"
#include "ui_form1.h"

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

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

void setLabelPixmap ( const QPixmap &  pic ) {

  Form1.ui->labelk->setPixmap(pic);


}

this is the header file form1.h

#ifndef FORM1_H
#define FORM1_H

#include <QWidget>

namespace Ui {
    class Form1;
}

class Form1 : public QWidget
{
    Q_OBJECT

public:
    explicit Form1(QWidget *parent = 0);
    ~Form1();
    void setLabelPixmap(const QPixmap &);

private:
    Ui::Form1 *ui;
};

#endif // FORM1_H

I Got this error :(Edited)

..\Project\form1.cpp: In function 'void setLabelPixmap(const QPixmap&)':

so How can i solve this problem ... i want to set the Qlabel of form1 from Form, or should i do it somehow in From1 ???


The compiler says that ui->Form1 doesn't work, because there is no Form1 in the object ui points to.

This could be because you have misspelled it (form1, From1, form, Form2?), or because ui isn't pointing to the object it should.


The statement "ui->Form1" assumes that there is something in "form" called Form1 that you added in the designer. From your explanation and your code that doesn't appear to be the case. What I think you want instead is something like

Form1* f= new Form1();
f->ui->labelk->setPixmap(mypix);  // Setting the png to the label inside Form1
f->showFullScreen();
...

Which is setting the image for the label in your new Form1 which you are about to display.

However, normally the ui pointer is not made public. Kind of bad practice. So I would suggest that you create a public method in your Form1 class like

void  setLabelPixmap ( const QPixmap & )

and pass the pixmap as just before you show it.

f->setLabelPixmap(mypix);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜