Не получается разобраться с слотом для автоматического заполнения полей.
Имеется следующий код, реализующий простенькую форму с 5 QLabel, 5 QLineEdit и 3 QPushButton:
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QVBoxLayout>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
public slots:
void setValue();
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
QGridLayout *grid = new QGridLayout;
QLabel *x1label = new QLabel(tr("x1="));
QLabel *y1label = new QLabel(tr("y1="));
QLabel *x2label = new QLabel(tr("x2="));
QLabel *y2label = new QLabel(tr("y2="));
QLabel *slabel = new QLabel(tr("S="));
QLineEdit *x1edit = new QLineEdit;
QLineEdit *y1edit = new QLineEdit;
QLineEdit *x2edit = new QLineEdit;
QLineEdit *y2edit = new QLineEdit;
QLineEdit *sedit = new QLineEdit;
QPushButton *automode = new QPushButton(tr("Auto"));
QPushButton *resetmode = new QPushButton(tr("Reset"));
QPushButton *gomode = new QPushButton(tr("Go!"));
grid->addWidget(x1label, 0, 0);
grid->addWidget(y1label, 1, 0);
grid->addWidget(x2label, 2, 0);
grid->addWidget(y2label, 3, 0);
grid->addWidget(slabel, 4, 0);
grid->addWidget(x1edit, 0, 1);
grid->addWidget(y1edit, 1, 1);
grid->addWidget(x2edit, 2, 1);
grid->addWidget(y2edit, 3, 1);
grid->addWidget(sedit, 4, 1);
grid->addWidget(automode, 5, 1);
grid->addWidget(resetmode, 6, 1);
grid->addWidget(gomode, 7, 1);
layout->addLayout(grid);
setLayout(layout);
connect(resetmode, SIGNAL(clicked()), x1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), x2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), sedit, SLOT(clear()));
connect(automode, SIGNAL(clicked()), x1edit, SLOT(setValue()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
И опять нет макроса Q_OBJECT и в конце "include main.moc"
Я вообще советую не создавать динамически кнопочки. Лучше .ui файл создать и как сказал Юрий вынести класс в отдельные файлы. И тоже верно подмечено setValue - слот MyWidget'a, а не x1edit. И возьми за правило передавать parent'a дочерним объектам, а то рано или поздно нарвешься на утечку памяти. А то ведь когда наступит время перепроектировать интерфейс придется угробить не мало времени, чтобы все переделать.
void MyWidget::setValue()
{
x1edit.setText("x1");
y1edit.setText("y1");
x2edit.setText("x2");
};
main.cpp: In member function ‘void MyWidget::setValue()’:
main.cpp:20: error: ‘x1edit’ was not declared in this scope
make: *** [main.o] Ошибка 1
Ну правильно пишет, ведь у тебя объекты объявляются и создаются в конструкторе и только в пределах видимости (скобок {}) о них известно. Как временное решение могу посоветовать объявление указателей вынести в секцию данных класса.
private:
QVBoxLayout *layout;
...
Ткните плиз пальцем, где и что не так:
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QVBoxLayout>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
private:
QVBoxLayout *layout;
QGridLayout *grid;
QLineEdit *x1edit;
public slots:
void setValue();
};
void MyWidget::setValue()
{
x1edit->setText("123");
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
//QVBoxLayout *layout = new QVBoxLayout;
//QGridLayout *grid = new QGridLayout;
QLabel *x1label = new QLabel(tr("x1="));
QLabel *y1label = new QLabel(tr("y1="));
QLabel *x2label = new QLabel(tr("x2="));
QLabel *y2label = new QLabel(tr("y2="));
QLabel *slabel = new QLabel(tr("S="));
//QLineEdit *x1edit = new QLineEdit;
QLineEdit *y1edit = new QLineEdit;
QLineEdit *x2edit = new QLineEdit;
QLineEdit *y2edit = new QLineEdit;
QLineEdit *sedit = new QLineEdit;
QPushButton *automode = new QPushButton(tr("Auto"));
QPushButton *resetmode = new QPushButton(tr("Reset"));
QPushButton *gomode = new QPushButton(tr("Go!"));
grid->addWidget(x1label, 0, 0);
grid->addWidget(y1label, 1, 0);
grid->addWidget(x2label, 2, 0);
grid->addWidget(y2label, 3, 0);
grid->addWidget(slabel, 4, 0);
grid->addWidget(x1edit, 0, 1);
grid->addWidget(y1edit, 1, 1);
grid->addWidget(x2edit, 2, 1);
grid->addWidget(y2edit, 3, 1);
grid->addWidget(sedit, 4, 1);
grid->addWidget(automode, 5, 1);
grid->addWidget(resetmode, 6, 1);
grid->addWidget(gomode, 7, 1);
layout->addLayout(grid);
setLayout(layout);
connect(resetmode, SIGNAL(clicked()), x1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), x2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), sedit, SLOT(clear()));
connect(automode, SIGNAL(clicked()), x1edit, SLOT(setValue()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
connect(automode, SIGNAL(clicked()), this, SLOT(setValue()));
Не работает. Вообще не появляется окно, хотя компилится все нормально...
Q_OBJECT добавил, main.moc тоже добавил. Не компилируется теперь.
Погуглил про main.moc - как я понял это в моем случае и не нужно...
FladeX, ты победил свое творение или нет?
Нет..
На одном форуме вроде подсказали как надо, но все равно не до конца.. Сейчас пытаюсь доделать тот вариант.
Собираю
qmake -project
qmake
make
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QVBoxLayout>
#include <main.moc>
class MyWidget : public QWidget
Q_OBJECT
{
public:
MyWidget(QWidget *parent = 0);
//
private:
QLineEdit *x1edit;
//
public slots:
void setValue();
};
void MyWidget::setValue()
{
x1edit->setText("123");
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
QGridLayout *grid = new QGridLayout;
QLabel *x1label = new QLabel(tr("x1="));
QLabel *y1label = new QLabel(tr("y1="));
QLabel *x2label = new QLabel(tr("x2="));
QLabel *y2label = new QLabel(tr("y2="));
QLabel *slabel = new QLabel(tr("S="));
x1edit = new QLineEdit;
QLineEdit *y1edit = new QLineEdit;
QLineEdit *x2edit = new QLineEdit;
QLineEdit *y2edit = new QLineEdit;
QLineEdit *sedit = new QLineEdit;
QPushButton *automode = new QPushButton(tr("Auto"));
QPushButton *resetmode = new QPushButton(tr("Reset"));
QPushButton *gomode = new QPushButton(tr("Go!"));
grid->addWidget(x1label, 0, 0);
grid->addWidget(y1label, 1, 0);
grid->addWidget(x2label, 2, 0);
grid->addWidget(y2label, 3, 0);
grid->addWidget(slabel, 4, 0);
grid->addWidget(x1edit, 0, 1);
grid->addWidget(y1edit, 1, 1);
grid->addWidget(x2edit, 2, 1);
grid->addWidget(y2edit, 3, 1);
grid->addWidget(sedit, 4, 1);
grid->addWidget(automode, 5, 1);
grid->addWidget(resetmode, 6, 1);
grid->addWidget(gomode, 7, 1);
layout->addLayout(grid);
setLayout(layout);
connect(resetmode, SIGNAL(clicked()), x1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y1edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), x2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), y2edit, SLOT(clear()));
connect(resetmode, SIGNAL(clicked()), sedit, SLOT(clear()));
connect(automode, SIGNAL(clicked()), this, SLOT(setValue()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
linux-85og:/home/fladex/Qt/Rectangle # qmake -project
linux-85og:/home/fladex/Qt/Rectangle # qmake
linux-85og:/home/fladex/Qt/Rectangle # make
/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -I. -I. -I. main.cpp -o main.moc
main.cpp:0: Warning: No relevant classes found. No output generated.
g++ -c -pipe -O2 -march=i586 -mtune=i686 -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o main.o main.cpp
main.cpp:12: error: expected `{' before ‘public’
main.cpp:12: error: expected unqualified-id before ‘public’
main.cpp:12: error: uninitialized const ‘staticMetaObject’
main.cpp:12: error: virtual outside class declaration
main.cpp:12: error: non-member function ‘const QMetaObject* metaObject()’ cannot have cv-qualifier
main.cpp:12: error: virtual outside class declaration
main.cpp:12: error: virtual outside class declaration
main.cpp:12: error: expected unqualified-id before ‘private’
main.cpp:24: error: invalid use of incomplete type ‘class MyWidget’
main.cpp:11: error: forward declaration of ‘class MyWidget’
main.cpp:29: error: invalid use of incomplete type ‘class MyWidget’
main.cpp:11: error: forward declaration of ‘class MyWidget’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:82: error: aggregate ‘MyWidget widget’ has incomplete type and cannot be defined
make: *** [main.o] Ошибка 1
FladeX, ошибка простая, она связана со знанием работы MOC,
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)