3

I am using the Qt add in for visual studio. Now I made a simple push button using Qt designer, and I want to use that push button so that it runs a function with a certain inputparameter when it is pressed, and then displays the result that is printed by the function.

The function that I want to run uses the eigen library so requires #include <Eigen/Dense> and should be called as follows:

void coef(Eigen::Matrix<long double, Dynamic, Dynamic> vector, Eigen::Matrix<long double, Dynamic, Dynamic> Matrix)

After I made the push button in Qt designer it automatically already added some code to my header file. Now I adjusted this header file to the following:

#ifndef QTDEMO_H

#define QTDEMO_H

#include <QtWidgets/QMainWindow>
#include "ui_qtdemo.h"

class qtdemo : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::qtdemoClass ui;

// begin new code
public slots:
    void on_btnHello_clicked() {
        ui.btnHello->coef(v, A); // v and A are defined in main.cpp, so not in this header file
    }
// end new code

};

#endif // QTDEMO_H

I know this would of course not work because

  1. the Eigen libary is unknown to this header,
  2. v and A are unknown to this header
  3. 3) the function coef() is unknown to this header file.

However, I am unexperienced with using header files, so I don't know what to do to make it work. Could anyone please help? Thanks in advance.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
dreamer
  • 1,192
  • 5
  • 20
  • 42

1 Answers1

2

You would need to do the following:

1) #include <Eigen/Dense> in this file.

2) ui.btnHello->coef(v, A); -> coef(v, A);

3) Move v and A as const member variables into this class or make them static here. Although, it would be better to move the implementation into your qtdemo.cpp source file and only leave the declaration in the header.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Thanks :). How would you suggest doing 3) the most handily? A and v are generated by another function in main.cpp – dreamer May 24 '14 at 16:18
  • Thanks again. I don't really get the last part yet, which part of the implementation would you move to the .cpp file? And what exactly would you keep in the header? – dreamer May 24 '14 at 16:22
  • @dreamer: you have `v` and `A` in the main.cpp, whereas you need it in this header... move those variables into this header and include this header from the main.cpp which you would need to do it anyhow. – László Papp May 24 '14 at 16:24
  • This seems to work, but the issue that I still have is that the the output of `coef(v,A)` is not printed in the interface (I naively tried to add `cout` statements in the `coef()` function but that also doesn't work). Is there any way to show the output in the interface after the function call? – dreamer May 25 '14 at 09:49
  • @dreamer: e.g. add a QLabel and call the setText(..) method on it with your desired string. – László Papp May 25 '14 at 09:50