user3794592
user3794592

Reputation: 183

QT Creator Main.cpp MainWindow.cpp

I'm currently working on my project for my Master thesis in Mechatronics/Robotics. The goal of y project is to read in a .stl-File and calculate the path for an industrial robot.

Till now everything worked fine for me, but now my professor wants me to develop a GUI, because till now I was just using the command window and wrote all parameters manual. Now I'm working with Qt Creator and developed a simple GUI for my project.

In this interface I got a RadioButton for ascii files. In order my functions work I have to determine if the user is entering a ascii file or an binary file. But here's my first problem. In the command window I just check the argv[] for the string "-ascii". If the user enters this, a flag is set to false.

if(0 == strcmp(argv[i], "-ascii")) {
    isBinaryFormat = false;
}

Now I just want to do the same int the GUI. If the RadioButton is checked flag is set to false. So I wrote the following in the main.cpp file

if(ui->radioButton->isChecked()) {
    isBinaryFormat = false;
}

But ui is unknown in the main function. After searching for help on google I just found tutorials writing the code in the mainwindow.cpp file. But how can I send the information form the mainwindow file to my main function in the main.cpp file.

A second question would be, if I use the QFileDialog::getOpenFilename method, how can I hand the file name to my other functions. The idea is, the user selects a file anywhere on his PC, and the program opens the file and processes it. But here I got the same problem. I can brows for a file, but how can I transfer the information from the mainwindow.cpp to my main.cpp.

I'm thankful for any help I get. Very grateful a lonely coder

Upvotes: 0

Views: 1830

Answers (3)

LogicStuff
LogicStuff

Reputation: 19607

It's not just about the files, there's a class, too. Learn about them. Solution is to add a getter to your MainWindow class that will return whether the radioButton is checked:

class MainWindow : public QMainWindow
{
    public:
        // optionally, move implementation in the source file
        bool isBinaryFormatChecked() const
        {
            return ui->radioButton->isChecked();
        }

    // other stuff ...
};

And then you can access it in your main like window.isBinaryFormatChecked() or
window->isBinaryFormatChecked() depending on whether you have a pointer or not. Another way would be to make ui in your MainWindow public, so you could access the whole user interface, but that breaks proper encapsulation.

Upvotes: 1

LiamT
LiamT

Reputation: 112

I think you need to go through a few of the (excellent in my opinion) examples supplied with Qt before attempting to integrate your already working console code.

Essentially you really don't want to do that check in the main.cpp, but if you must you could have it in a public function of the mainwindow and call that from your main.cpp file. But then that doesn't really make sense as you don't want to check whether the appropriate radio button is set until the user inputs something. You're going to have to read up on event based programming.

Upvotes: 0

deW1
deW1

Reputation: 5660

First of all you don't write UI Code in the main.cpp. You write it where the MainWindow Class is so in MainWindow.cpp and MainWindow.h.

Then your ui-> will work because it then has access to that namespace.

I don't see why you would have functions in Main.cpp? Without seeing more code you're not likely to get an answer to that.

If you want to use external functions in your classes either declare the methods in the class directly or create a new file like global_function.h and .cpp which you can include in your class. ( don't forget the header guards )

Also shouldn't that code look like this:

if(!ui->radioButton->isChecked())
{
    isBinaryFormat = false;
}

because of:

If the RadioButton is checked flag is set to false.


QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                "/home",
                                                tr("Images (*.png *.xpm *.jpg)"));

getOpenFileName( ) will return a string containing the path and filename of the selected file which you can pass to your functions then.

Please read some more about how to use Qt.

Upvotes: 1

Related Questions