简体   繁体   中英

Adding .ui file to an existing class in QT

could anyone tell me the steps on how to add a .ui file to an existing class in Visual? Firstly I added a new item in Visual, then I chose "QT Dialog Form File" option and I then I created Dialog Form I desire in QT Designer.

My .h file:

(...)
#include "ui_Serial.h"


class Serial : public QWidget
{
    Q_OBJECT

public:
    Serial(QWidget *parent);
    ~Serial();

    Ui::Serial *ui;
(...)

My problem is, that I can't use setup ui function:

Serial::Serial(QWidget *parent)
: QWidget(parent)
{
    serial = new QSerialPort(this);
    ui->setupUi(this);

}

I am getting this error: cannot convert argument 1 from 'Serial *' to 'QDialog *

How I can get pass that? Any ideas? Greetings

Each Template has a default class because when the .ui is built, class commands are embedded.

  • If you use the template Widget your class should be QWidget .
  • If you use the template Dialog with Buttons Bottom , Dialog with Buttons Right , Dialog without Buttons your class should be QDialog .
  • If you use the template MainWindow your class should be QMainWindow .

So we conclude in your case that you should use a class that inherits from QDialog:

*.h

#include "ui_Serial.h"


class Serial : public QDialog
{
    Q_OBJECT

public:
    Serial(QWidget *parent=0);
    ~Serial();

    Ui::Serial *ui;
}

*.cpp

Serial::Serial(QWidget *parent):QDialog(parent)
{
    serial = new QSerialPort(this);
    ui->setupUi(this);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM