-5

I write in header file private slots and the compiler gives an error:

d:\qtproject\new123\mainwindow.h:31: error: C2059: syntax error : 'public'

Please help me. I changed to public but no difference.When I clear 'public/private slots' no error comes , but writes loading D:\Qtproject\new123\debug\new123.exe... QObject::connect: No such slot QPushButton::changed() in main.cpp:18 QObject::connect: No such slot QPushButton::moved() in main.cpp:27

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
namespace Ui {
  class MainWindow;
}
class MainWindow : public QMainWindow
{
  Q_OBJECT
  public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
  private slots:
    void changed();
    void moved();
    signals:
    void clicked();
  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);

}

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

void moved()
{
    QPushButton a;
     a.move(100,100);
}
void changed()

{   QPushButton g;
    g.setStyleSheet("QPushButton { background-color : white; color :blue; }");
}

main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QPushButton>



int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    MainWindow w;

    QPushButton s("Change Color", &w);
    s.setStyleSheet("QPushButton { background-color : white; color :pink; }"
                    "QPushButton:pressed { color: blue; }");
    QObject::connect(&s, SIGNAL(clicked()), &s, SLOT(changed()));
    QPushButton d("Quit", &w);
    d.setStyleSheet("QPushButton { background-color : white; color :black; }");
    QObject::connect(&d, SIGNAL(clicked()), qApp, SLOT(quit()));
    d.move(100,0);
    QPushButton f("Move Button", &w);
    f.move(200,0);
    f.setStyleSheet("QPushButton { background-color : white; color :green; }");

    QObject::connect(&f, SIGNAL(clicked()), &f, SLOT(moved()));

    w.show();

    return a.exec();

}
kawake123
  • 53
  • 8
  • please help, I started study QT and need your help – kawake123 May 08 '15 at 20:41
  • 1
    Relevant code please. – AndyG May 08 '15 at 20:41
  • #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void changed(); void moved(); signals: void clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H – kawake123 May 08 '15 at 20:42
  • 1
    Please edit your question and add the code. – drescherjm May 08 '15 at 20:43
  • 2
    The error message talks about a problem on line 31. The snippet above doesn't have 31 lines. – Mat May 08 '15 at 21:04
  • Help us help you. Post a minimal but complete program that demonstrates the problem. Show us all the error messages. – metal May 08 '15 at 21:10
  • before #endif is the 31th line. when I clear ''public slots'' no error comes, but writes – kawake123 May 08 '15 at 21:11
  • I copied the whole code, can you help? – kawake123 May 08 '15 at 21:22

1 Answers1

1

You need to learn C++ before you learn Qt.

You declare moved() and changed() as methods of MainWindow, but then define them as free functions in the source file. You then attempt to connect the QPushButton::clicked() to these slots, but tell the connect(..) method that the slots belong to the QPushButton instead of MainWindow.

In your moved() and changed() functions you create a QPushButton in each, but don't give them a parent or add them to a layout (so they won't be visible). You also create them on the stack, so they are destroyed as the functions end.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • yes I learn everything and everytime)).thank You, but I am confused, I want to create a Button, click on it and change the text color, and create other button, click on it and move it.Can You give me an example how to do it, or code? – kawake123 May 09 '15 at 16:16
  • @kawake123 No. SO is not a software consultancy, there are enormous numbers of tutorials and examples at the Qt website (http://doc.qt.io/qt-5/qtexamplesandtutorials.html). You can ask a new question when have a _specific_ problem. – cmannett85 May 09 '15 at 16:45