Hello,
I would like to connect a slider I have created in "Design mode" with an own slot I have written into a new class.
The slider is named g_sld.
I get the error. "expected primary-expression before '->' token" in the main.cpp
Google couldnt help me and as I am a C++ beginner I dont unterstand the mistake.
Can anybody tell me whats wrong?
Thank you very much :-)
See the code following:
Headers:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // 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:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#ifndef MYSLOTS_H
#define MYSLOTS_H
#include <QObject>
{
Q_OBJECT
public:
explicit mySlots
(QObject *parent
= 0);
signals:
public slots:
void writeValue (int);
private:
int val;
};
#endif // MYSLOTS_H
#ifndef MYSLOTS_H
#define MYSLOTS_H
#include <QObject>
class mySlots : public QObject
{
Q_OBJECT
public:
explicit mySlots(QObject *parent = 0);
signals:
public slots:
void writeValue (int);
private:
int val;
};
#endif // MYSLOTS_H
To copy to clipboard, switch view to plain text mode
Sources:
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "myslots.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
QObject::connect(MainWindow
->g_sld,
SIGNAL(valueChanged
(int)),
SLOT(writeValue
(int)));
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
#include "myslots.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QObject::connect(MainWindow->g_sld, SIGNAL(valueChanged (int)),SLOT(writeValue(int)));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mywindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace std;
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
myslots.cpp
#include "myslots.h"
#include <iostream>
using namespace std;
mySlots
::mySlots(QObject *parent
) :{
int val=0;
}
void mySlots::writeValue (int x)
{
if (x != val)
{
cout << x << endl;
val = x;
}
}
#include "myslots.h"
#include <iostream>
using namespace std;
mySlots::mySlots(QObject *parent) :
QObject(parent)
{
int val=0;
}
void mySlots::writeValue (int x)
{
if (x != val)
{
cout << x << endl;
val = x;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks