Declaration of function and use it
-
Hello there
i am new in qt working with opencv with qt tool.
i am creating gui for image processing in qt tool.
i need to define function and use it with my push button.
how can i do it.
please tell me
waiting -
Do you mean like this:
ReturnType ClassName::nameOfFunction (ParameterType parameter){
// What the function should do
} -
Hello there
i am new in qt working with opencv with qt tool.
i am creating gui for image processing in qt tool.
i need to define function and use it with my push button.
how can i do it.
please tell me
waiting@gauravsharma0190 Do you mean you need a function which is called when you press a button?
If so then please read the documentation: http://doc.qt.io/qt-5.7/signalsandslots.html -
Dear sir,
i have a function static void onTrackball(int, void*)
and its defination.
i declare it in mainwindow.h as private.
and in mainwindow i declare it as void Mainwindow::onTrackball(int,void*)
but error occurs.
it doesn't build. -
Dear sir,
i have a function static void onTrackball(int, void*)
and its defination.
i declare it in mainwindow.h as private.
and in mainwindow i declare it as void Mainwindow::onTrackball(int,void*)
but error occurs.
it doesn't build.@gauravsharma0190 Can you post your code and the error message? Else we cannot help you.
One note onTrackball should not be static if you want to use it as slot.
Another note: if onTrackball is a slot for QPushButton::clicked() signal then you have to remove the parameters, because clicked() signal does not have any parameters. -
Thanks for kind reply sir
here is the code i am sending you.#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h> using namespace cv; /// Global variables Mat src, src_gray; Mat dst, detected_edges; int edgeThresh = 1; int lowThreshold; int const max_lowThreshold = 100; int ratio = 3; int kernel_size = 3; char* window_name = "Edge Map"; /** * @function CannyThreshold * @brief Trackbar callback - Canny thresholds input with a ratio 1:3 */ void CannyThreshold(int, void*) { /// Reduce noise with a kernel 3x3 blur( src_gray, detected_edges, Size(3,3) ); /// Canny detector Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size ); /// Using Canny's output as a mask, we display our result dst = Scalar::all(0); src.copyTo( dst, detected_edges); imshow( window_name, dst ); } /** @function main */ int main( int argc, char** argv ) { /// Load an image src = imread( argv[1] ); if( !src.data ) { return -1; } /// Create a matrix of the same type and size as src (for dst) dst.create( src.size(), src.type() ); /// Convert the image to grayscale cvtColor( src, src_gray, CV_BGR2GRAY ); /// Create a window namedWindow( window_name, CV_WINDOW_AUTOSIZE ); /// Create a Trackbar for user to enter threshold createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold ); /// Show the image CannyThreshold(0, 0); /// Wait until user exit program by pressing a key waitKey(0); return 0; }
in that i want to make as when button is pressed this code and function will run.
But i don't know where to declare function and how to call it in qt.
When i declare function void on track ball(int void*) in mainwindow.h
and define as void mainwindow::ontrackball(int,void*) and build it following outcome
declaration of 'void mainwindow::ontrackball(int void) outside class is not definition
expected unqulified-id before '{' token.[Edit: Added code formatting tags. ~kshegunov]
-
Do you mean like this:
ReturnType ClassName::nameOfFunction (ParameterType parameter){
// What the function should do
}hey i done that but after build it says declaration of function outside of class is not defination.
-
hey i done that but after build it says declaration of function outside of class is not defination.
@gauravsharma0190 Can you please post the code which causes the compiler error? The code you posted is not of interest. Where is mainwindow::ontrackball? Please post mainwindow.h and mainwindow.cpp.
-
hey i done that but after build it says declaration of function outside of class is not defination.
@gauravsharma0190 Did you put void on_track_ball(int void*) inside mainwindow class in mainwindow.h? The error message sounds like you put it outside of mainwindow. It should be like:
// mainwindow.h class mainwindow { private: void on_track_ball(int, void*); } // mainwindow.cpp void mainwindow::on_track_ball(int, void*) { }
-
hey i done that but after build it says declaration of function outside of class is not defination.
@gauravsharma0190 The code you posted does not use Qt as far as I can see. You will need to change more if you want to use Qt.
-
@gauravsharma0190 The code you posted does not use Qt as far as I can see. You will need to change more if you want to use Qt.
@jsulm
Yes i did the same but error occurs.
i couldnt find where is problem -
@jsulm
Yes i did the same but error occurs.
i couldnt find where is problem@gauravsharma0190 That's why I'm asking for the code! Without it I cannot find the problem as well.
-
So please tell me what i send you sir.
i give you my all the code .
Now please tell me only that how to invoke code when i click the button -
So please tell me what i send you sir.
i give you my all the code .
Now please tell me only that how to invoke code when i click the button@gauravsharma0190 I already said that: mainwindow.h and mainwindow.cpp
I provided you a link where you can learn how signals and slots in Qt work, here it is again: http://doc.qt.io/qt-5.7/signalsandslots.html
Please read it! Else you will not be able to use Qt much - signals/slots is one of the most important concepts in Qt.
Short description: QPushButton has a signal clicked() (see http://doc.qt.io/qt-5/qabstractbutton.html#clicked). This signal is emitted when user presses the button. You need to connect a slot to that signal, then this slot will be called when you press the button. -
So please tell me what i send you sir.
i give you my all the code .
Now please tell me only that how to invoke code when i click the button@gauravsharma0190 A small example application with a button:
// main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } // mainwindow.h #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(); private slots: void doSomethingWhenButtonPressed(bool); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H // mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(doSomethingWhenButtonPressed(bool))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::doSomethingWhenButtonPressed(bool) { qDebug() << "Button pressed!"; }
-
@gauravsharma0190 A small example application with a button:
// main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } // mainwindow.h #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(); private slots: void doSomethingWhenButtonPressed(bool); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H // mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(doSomethingWhenButtonPressed(bool))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::doSomethingWhenButtonPressed(bool) { qDebug() << "Button pressed!"; }
@jsulm
i got it tht how to use push button.
using connect i know it because i did it with my code.
but i want to know how to declare and defination of function in qt and use these in pushbutton.
Like we do in c as declare and defination of function and use it main()
as in c++ declare and defination and use it in main function or classes.
please give me a simple example or link.like
-
@jsulm
i got it tht how to use push button.
using connect i know it because i did it with my code.
but i want to know how to declare and defination of function in qt and use these in pushbutton.
Like we do in c as declare and defination of function and use it main()
as in c++ declare and defination and use it in main function or classes.
please give me a simple example or link.like
@gauravsharma0190 I don't understand the problem. Qt is not a programming language. You use C++ with qt. So you define and use your function/method as any other function/method in C++.
I already gave you an example showing how to use a push button to execute a method when the button is clicked. Isn't that what you want to do?
Please explain what is the problem! Maybe some code? -
So please tell me what i send you sir.
i give you my all the code .
Now please tell me only that how to invoke code when i click the buttonHi
As @jsulm says, its a bit unclear what you ask.
You can use functions just like you would in c, but in c++ gui program,
normally main is the not the center of the application. QMainwindow is or some other class.
Maybe this helps://-------------------- //std c program //-------------------- void myfunc() {}; void main() { myfunc(); } //-------------------- //c++ function being a class member ( lives in class) //-------------------- class Mainwindow { public: void myfunc(); }; (in cpp) Mainwindow::myfunc() { //code } //-------------------- //c++ with "outside" function ( non member function) //-------------------- (in .h) void outsidefunc(); // define class Mainwindow { public: void myfunc(); }; (in cpp) void outsidefunc() { // implement } Mainwindow::myfunc() { outsidefunc(); }
-
Yes sir see i have a c code of opencv which process the image.
Now my problem is i want to use this code with qt as when button is pressed my push button calls the function of that C code in which main part of Process is defined.
Here is the C code.
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>using namespace cv;
/// Global variables
Mat src, src_gray;
Mat dst, detected_edges;int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";/**
- @function CannyThreshold
- @brief Trackbar callback - Canny thresholds input with a ratio 1:3
/
void CannyThreshold(int, void)
{
/// Reduce noise with a kernel 3x3
blur( src_gray, detected_edges, Size(3,3) );
/// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );/// Using Canny's output as a mask, we display our result
dst = Scalar::all(0);src.copyTo( dst, detected_edges);
imshow( window_name, dst );
}/** @function main /
int main( int argc, char* argv )
{
/// Load an image
src = imread( argv[1] );if( !src.data )
{ return -1; }/// Create a matrix of the same type and size as src (for dst)
dst.create( src.size(), src.type() );/// Convert the image to grayscale
cvtColor( src, src_gray, CV_BGR2GRAY );/// Create a window
namedWindow( window_name, CV_WINDOW_AUTOSIZE );/// Create a Trackbar for user to enter threshold
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );/// Show the image
CannyThreshold(0, 0);/// Wait until user exit program by pressing a key
waitKey(0);return 0;
}
Now i make a Push button Name Process_image in MainWindow.ui and connect to slot as clicked() by right clicking of mouse.
here comes the slot to mainwindow.cpp
void mainwidow::Process_image_clicke()
{
}
Now i don't understand how to make function of my C code and use it in Pusebutton. -
well you should make a pair of .h and .cpp files for the opencv stuff
lets call it myopenvc.h and myopenvc.cppin h. you can declare the function u want rest of program to use
void setupOPENVC();
and in .cpp
void setupOPENVC() {
// the code from the old c- main. !
src = imread( argv[1] );
xxxx
}then in Mainwindow.cpp
#include "myopencv.h"and now u can call your function
void mainwidow::Process_image_clicke()
{
setupOPENVC();
}ps:
In event driven programs. ( like Qt)
stuff like
waitKey(0);
or FOREVER loops are not to be used as it
will freeze the program and it stops working.