I am new with QT and c++. So I do this tutorial Tutorial
I copied everything, but qt can't compile this.
//imageviewer.cpp
#include "imageviewer.h"
#include "ui_imageviewer.h"
ImageViewer::ImageViewer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ImageViewer)
{
ui->setupUi(this);
QImage image("C:/TEST/GoldenGate.png");
ui->imageLabel->setPixmap(QPixmap::fromImage(image));
}
ImageViewer::~ImageViewer()
{
delete ui;
}
//imageviewer.h
#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H
#include <QMainWindow>
#include <QLabel>
#include <QScrollArea>
namespace Ui {
class ImageViewer;
}
class ImageViewer : public QMainWindow
{
Q_OBJECT
public:
explicit ImageViewer(QWidget *parent = 0);
~ImageViewer();
private:
QLabel *imageLabel;
QScrollArea *scrollArea;
Ui::ImageViewer *ui;
};
#endif // IMAGEVIEWER_H
And got this:
'class Ui::ImageViewer' has no member named 'imageLabel'
I can't understand why it doesnt't see this variable.
Your Ui::ImageViewer is not complete .. it does not have 'imageLabel' added. Open the form in design mode and add QLabel, name it 'imageLabel' and try again.
Otherwise, follow tutorial to the end, it adds the QLabel 'imageLabel' dynamically in imageview.cpp.
// imageview.cpp
#include "imageviewer.h"
#include "ui_imageviewer.h"
ImageViewer::ImageViewer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ImageViewer)
{
ui->setupUi(this);
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
setCentralWidget(scrollArea);
setWindowTitle(tr("Image Viewer"));
resize(500, 400);
}
Related
When I try to add any new widget to layout my program crashes. What could be the problem?
I have a pointer to QLabel as a data member of MainWindow. In constructor I allocate memory for it and that try to add it to layout. I really don't know what is the problem, cause I did this method before and it worked.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QLabel* tempLabel;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
tempLabel = new QLabel("QLabel 2");
ui->widget->layout()->addWidget(tempLabel);
ui->setupUi(this);
}
MainWindow::~MainWindow() {
delete ui;
}
Error msg
I dynamically add a button, for example:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *btn = new QPushButton(this);
btn->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
btn->move(150,150);
btn->resize(100,100);
btn->show();
}
how do I use this button in another void in Qt??
for example, I want to click another button to delete this dynamically added button.
your button is defined inside the constructor so after you do
btn->resize(100,100);
btn->show();
and exit the constructor you has nothing in the hand to manipulate the button (which is what you need now)
one way to solve this is to declare the button as a class member and then you can use its name to change it as much as you need.
edit:
the header:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QPushButton *myButton{nullptr};
};
#endif // MAINWINDOW_H
the cpp:
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myButton = new QPushButton("some button");
myButton->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
myButton->move(150,150);
myButton->resize(100,100);
}
MainWindow::~MainWindow()
{
delete ui;
delete myButton;
}
void MainWindow::on_pushButton_2_clicked()
{
myButton->show();
}
void MainWindow::on_pushButton_clicked()
{
// close or do what ever you want with it
myButton->close();
}
Welcome, I have a question about Qt's windows operations. I have a simple app which contains 2 windows:
MainWindow - includes push button, can't be resizeable,
AdminWindow - includes label, can be resizeable.
When I click push button, it should open AdminWindow and hide MainWindow. I made the app and it seems to work but when I open the AdminWindow, the application icon which is located in windows's bottom bar is missing. How can I fix it?
Icon is showed when MainWindow is opened:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "adminwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButtonOpenAdmin_clicked();
private:
Ui::MainWindow *ui;
AdminWindow *adminWindow;
};
#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);
}
MainWindow::~MainWindow()
{
delete ui;
}
// hides MainWindow and show AdminWindow
void MainWindow::on_pushButtonOpenAdmin_clicked()
{
hide();
adminWindow = new AdminWindow(this);
adminWindow->show();
}
adminwindow.h
#ifndef ADMINWINDOW_H
#define ADMINWINDOW_H
#include <QMainWindow>
namespace Ui {
class AdminWindow;
}
class AdminWindow : public QMainWindow
{
Q_OBJECT
public:
explicit AdminWindow(QWidget *parent = 0);
~AdminWindow();
private:
Ui::AdminWindow *ui;
};
#endif // ADMINWINDOW_H
adminwindow.cpp
#include "adminwindow.h"
#include "ui_adminwindow.h"
AdminWindow::AdminWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::AdminWindow)
{
ui->setupUi(this);
}
AdminWindow::~AdminWindow()
{
delete ui;
}
I am trying to access the ui elements of different class but getting error message. I've tried to do this for many hours but still failing, I feel that I am missing something simple.
I am trying to access an element "label" which is in form.h
mainwindow:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Form * elemForm = new Form(this);
elemForm->ui; // works
//elemForm->ui->label; // does not work
}
MainWindow::~MainWindow()
{
delete ui;
}
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
Ui::Form *ui;
private:
};
#endif // FORM_H
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
ui->label; //works fine here
}
Problem is in mainwindow file. I know this is inappropriate code, I'm just interested what I'm doing wrong. I am struggling to find whats wrong, any ideas?
In addition to form.h, you should also include ui_form.h in your MainWindow.h.
That's because the form elements are all defined in Ui::Form which is accessible by including :
#include "ui_form.h"
Please, try to think on what are you doing, and how to do things better.
Don't feel think that method explained by Nejat is controversial to OOP? If you are trying to use OOP, learn how to do it and use it.
To get deeper understanding on what am I talking about, please, read this short story about encapsulation.
Back to your example. Here is clean, simple and easy way to do what you want:
Your mainwindow.h:
#include "mainwindow.h"
#include "ui_mainwindow.h"
// Include only header of your Form, not ui_form.h!
#include "form.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Form* elemForm = new Form(this);
// Use public method of your Form object!
elemForm->changeUI();
}
Your form.h:
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
~Form();
void changeUI(); // here you can do all what you want with your UI
private:
Ui::Form *ui;
};
in form.cpp you'll have realization:
void Form::changeUI()
{
ui->... // do all what you want with UI
}
I am currently learning Qt and I seem to have run into a problem.
In my practice project I have 2 classes: MainWindow and Dialog.
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();
private slots:
void on_pushButtonDialog_clicked();
private:
Ui::MainWindow *ui;
Dialog *dialogInstance;
};
#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);
dialogInstance = new Dialog(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonDialog_clicked()
{
dialogInstance->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();
private:
Ui::Dialog *ui;
};
#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;
}
My goal is to input a value using the Dialog window, then have the value of that input shown on the MainWindow, I know how to pass variables around within the class using widgets, but I am not sure how to transfer variables between unrelated objects.
Any input would be of great help.
Try this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButtonDialog, SIGNAL(clicked()), this, SLOT(on_pushButtonDialog_clicked()));
dialogInstance = new Dialog(this);
}
// ...