pythonore
pythonore

Reputation: 37

How to add "member" to MainWindow class in Qt

So i have started to learn Qt, and i got hold of Qt5 C++ GUI Programming Cookbook. I wanted to make a simple video converter, and there is a example on how to do it in the book.

But executing the code from the book gives me an error:

'Ui::MainWindow' has no member named 'filePath'
     ui->filePath->setText(fileName);
         ^

So i guess i have to add filePath to the MainWindow, but since im new to Qt and C++ i dont know exactly how to do that.

Here is mainwindow.h

#include <QMainWindow>
#include <QFileDialog>
#include <QProcess>
#include <QMessageBox>
#include <QScrollBar>
#include <QDebug>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QProcess* process;
    QString outputText;
    QString fileName;
    QString outputFileName;

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
    void processStarted();
    void readyReadStandardOutput();
    void processFinished();
private:
    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)
{
    ui->setupUi(this);
    process = new QProcess(this);
    connect(process, SIGNAL(started()), this,
    SLOT(processStarted()));
    connect(process,SIGNAL(readyReadStandardOutput()),
    this,SLOT(readyReadStandardOutput()));
    connect(process, SIGNAL(finished(int)), this,
    SLOT(processFinished()));

}

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

void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, "OpenVideo", "", "Video Files (*.avi *.mp4 *.mov)");
    ui->filePath->setText(fileName);
}

void MainWindow::on_pushButton_2_clicked()
{
    QString ffmpeg = "C:/FFmpeg/bin/ffmpeg";
    QStringList arguments;
    fileName = ui->filePath->text();
    if (fileName != "")
    {
        QFileInfo fileInfo = QFile(fileName);
        outputFileName = fileInfo.patch() + "/" + fileInfo.completeBaseName();

        if (QFile::exists(fileName))
        {
            //0-AVI
            //1-MP4
            //2-MOV
            int format = ui -> fileFormat-> currentIndex();
            if (format ==0)
            {
                outputFileName += ".avi";
            }
            else if (format ==1)
            {
                outputFileName += ".mp4";
            }
            else if (format ==2)
            {
                outputFileName += ".mov";
            }
            qDebug()<<outputFileName<<format;
            arguments<< "-i"<<fileName<<outputFileName;
            qDebug()<<arguments;

            process->setProcessChannelMode(QProcess::MergedChannels);
            process->start(ffmpeg,arguments);
         }
         else
         {
            QMessageBox::warning(this,"Failed", "Failed to open video file.");
         }
      else
      {
           QMessageBox::warning(this,"Failed", "Failed to open video file.");
      }

    }
}
void MainWindow::processStarted()
{
qDebug() << "Process started.";
ui->browseButton->setEnabled(false);
ui->fileFormat->setEditable(false);
ui->convertButton->setEnabled(false);
}
void MainWindow::readyReadStandardOutput()
{
outputText += process->readAllStandardOutput();
ui->outputDisplay->setText(outputText);
ui->outputDisplay->verticalScrollBar()->setSliderPosition(ui->outputDisplay->verticalScrollBar()->maximum());
}
void MainWindow::processFinished()
{
qDebug() << "Process finished.";
if (QFile::exists(outputFileName))
{
QMessageBox::information(this, "Success", "Videosuccessfully converted.");
}
else
{
QMessageBox::information(this, "Failed", "Failed to convertvideo.");
}
ui->browseButton->setEnabled(true);
ui->fileFormat->setEditable(true);

And mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>404</width>
    <height>334</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLineEdit" name="lineEdit">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>30</y>
      <width>281</width>
      <height>21</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="text">
     <string>Browse</string>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>60</y>
      <width>371</width>
      <height>22</height>
     </rect>
    </property>
    <item>
     <property name="text">
      <string>AVI</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>MP4</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>MOV</string>
     </property>
    </item>
   </widget>
   <widget class="QTextEdit" name="textEdit">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>100</y>
      <width>371</width>
      <height>141</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>250</y>
      <width>371</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>Convert</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>404</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

Upvotes: 1

Views: 1825

Answers (1)

Tomaz Canabrava
Tomaz Canabrava

Reputation: 2408

The error is in the .ui file. You need to open Designer, click in your filePath, and change its name to filePath. Currently, it is textEdit.

All visual controls that you put on the .ui file via the Designer application should match the names you are using in the C++ code to access them.

Upvotes: 4

Related Questions