crossplatform.ru

Здравствуйте, гость ( Вход | Регистрация )

> Массив объектов, размерность неизвестна, трудности...
FladeX
  опции профиля:
сообщение 2.3.2009, 22:25
Сообщение #1


Студент
*

Группа: Участник
Сообщений: 67
Регистрация: 20.2.2009
Пользователь №: 564

Спасибо сказали: 0 раз(а)




Репутация:   0  


Нужно сделать массив объектов, причем размерность массива динамическая, то есть неизвестно сколько элементов будет в нем.
Что делал я:
Сначала описал объект:
class Triangle : public QWidget
{
    Q_OBJECT
public:
    int id;
    double x1;
    double y1;
    double x2;
    double y2;
    double x3;
    double y3;
};

Затем, немного погуглив, понял, что лучше всего использовать QList. Задаю массив так:
class cDrawField : public QWidget
{
    Q_OBJECT
public:
    QList<Triangle> triangle;
}

Вроде все правильно, а компилятор ругается на неизвестные переменные:
main.cpp: In constructor ‘cDrawField::cDrawField(QWidget*)’:
main.cpp:71: error: ‘class QList<Triangle>’ has no member named ‘id’
main.cpp:72: error: ‘class QList<Triangle>’ has no member named ‘x1’
main.cpp:73: error: ‘class QList<Triangle>’ has no member named ‘y1’
main.cpp:74: error: ‘class QList<Triangle>’ has no member named ‘x2’
main.cpp:75: error: ‘class QList<Triangle>’ has no member named ‘y2’
main.cpp:76: error: ‘class QList<Triangle>’ has no member named ‘x3’
main.cpp:77: error: ‘class QList<Triangle>’ has no member named ‘y3’

А где их прописывать, я так и не понял.. Подскажите, пожалуйста.
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение
 
Начать новую тему
Ответов
FladeX
  опции профиля:
сообщение 2.3.2009, 22:39
Сообщение #2


Студент
*

Группа: Участник
Сообщений: 67
Регистрация: 20.2.2009
Пользователь №: 564

Спасибо сказали: 0 раз(а)




Репутация:   0  


Не помогло...
Код не весь, взял только ключевые моменты.

Вообще весь код вот:

CODE

#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QString>
#include <QPainter>
#include <QPaintEvent>
#include <QPen>
#include <QFont>
#include <QList>
#include "math.h"

#define MOVE_VAL 50

class Triangle : public QWidget
{
Q_OBJECT
public:
int id;
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
};

class cDrawField : public QWidget
{
Q_OBJECT
public:
cDrawField(QWidget *parent = 0);
~cDrawField() {};
double tx1, ty1, tx2, ty2, ts;
double scale_val; // dlya mshtabirovaniya, koefficient
int x_val, y_val;
QPainter painter;
//Triangle triangle;
QList<Triangle*> triangle;
int counter;
protected:
void paintEvent(QPaintEvent *);
void triangleDraw(double tmpx1, double tmpy1, double tmpx2, double tmpy2, double tmpx3, double tmpy3);
private:
int triangle.id;
double triangle.x1;
double triangle.y1;
double triangle.x2;
double triangle.y2;
double triangle.x3;
double triangle.y3;
};

cDrawField::cDrawField(QWidget *parent)
: QWidget(parent)
{
scale_val = 1;

x_val = 0;
y_val = 0;

tx1 = 0;
ty1 = 0;
tx2 = 0;
ty2 = 0;
ts = 0;
triangle.id = 0;
triangle.x1 = 0;
triangle.y1 = 0;
triangle.x2 = 0;
triangle.y2 = 0;
triangle.x3 = 0;
triangle.y3 = 0;
counter = 0;

setMinimumSize(500, 500);
}

class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
public slots:
void setValue();
void drawRect();
void scaleMinus();
void scalePlus();
void moveLeft() { drawer->x_val -= MOVE_VAL; drawer->repaint(); };
void moveRight() { drawer->x_val += MOVE_VAL; drawer->repaint(); };
void moveUp() { drawer->y_val -= MOVE_VAL; drawer->repaint(); };
void moveDown() { drawer->y_val += MOVE_VAL; drawer->repaint(); };
private:
QLineEdit *x1edit;
QLineEdit *y1edit;
QLineEdit *x2edit;
QLineEdit *y2edit;
QLineEdit *sedit;
QPushButton *automode;
QPushButton *resetmode;
QPushButton *gomode;
QPushButton *minus;
QPushButton *plus;
QPushButton *b_left, *b_right,
*b_up, *b_down;
cDrawField *drawer;
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout;
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;
y1edit = new QLineEdit;
x2edit = new QLineEdit;
y2edit = new QLineEdit;
sedit = new QLineEdit;
automode = new QPushButton(tr("Auto"));
resetmode = new QPushButton(tr("Reset"));
gomode = new QPushButton(tr("Go!"));
minus = new QPushButton(tr("-"));
plus = new QPushButton(tr("+"));

b_left = new QPushButton(tr("left"));
b_right = new QPushButton(tr("right"));
b_up = new QPushButton(tr("up"));
b_down = new QPushButton(tr("down"));

drawer = new cDrawField;
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);
//grid->addWidget(drawer, 0, 2, 8, 3);
grid->setRowStretch(8,1);
grid->addWidget(minus, 9, 0);
grid->addWidget(plus, 9, 1);
grid->addWidget(b_up, 10, 0);
grid->addWidget(b_left, 10, 1);
grid->addWidget(b_down, 11, 0);
grid->addWidget(b_right, 11, 1);
//grid->setColumnStretch(2, 1);
//grid->setRowStretch(1,1);
layout->addLayout(grid);
layout->addWidget(drawer, 1);
//layout->setAlignment(Qt::AlignLeft);
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()));
connect(gomode, SIGNAL(clicked()), this, SLOT(drawRect()));
connect(minus, SIGNAL(clicked()), this, SLOT(scaleMinus()));
connect(plus, SIGNAL(clicked()), this, SLOT(scalePlus()));
connect(b_left, SIGNAL(clicked()), this, SLOT(moveLeft()));
connect(b_right, SIGNAL(clicked()), this, SLOT(moveRight()));
connect(b_up, SIGNAL(clicked()), this, SLOT(moveUp()));
connect(b_down, SIGNAL(clicked()), this, SLOT(moveDown()));
}

void MyWidget::setValue()
{
double tx1, ty1, tx2, ty2, ts;
QString sx1, sy1, sx2, sy2, ss;
tx1 = rand() % 500;
ty1 = rand() % 500;
tx2 = rand() % 500;
ty2 = rand() % 500;
ts = rand() % 100;
sx1 = QString::number(tx1);
sy1 = QString::number(ty1);
sx2 = QString::number(tx2);
sy2 = QString::number(ty2);
ss = QString::number(ts);
x1edit->setText(sx1);
y1edit->setText(sy1);
x2edit->setText(sx2);
y2edit->setText(sy2);
sedit->setText(ss);
};

void MyWidget::drawRect()
{
QString sx1, sy1, sx2, sy2, ss;
sx1 = x1edit->text();
sy1 = y1edit->text();
sx2 = x2edit->text();
sy2 = y2edit->text();
ss = sedit->text();
drawer->tx1 = sx1.toDouble();
drawer->ty1 = sy1.toDouble();
drawer->tx2 = sx2.toDouble();
drawer->ty2 = sy2.toDouble();
drawer->ts = ss.toDouble();
drawer->update();
};

void MyWidget::scaleMinus()
{
drawer->scale_val -= 0.15;
drawer->repaint();
};

void MyWidget::scalePlus()
{
drawer->scale_val += 0.15;
drawer->repaint();
};

void cDrawField::paintEvent(QPaintEvent *e)
{
counter = 0;
painter.begin(this);
//painter.translate(this->width()/2, this->height()/2);
//painter.scale(10, 10);
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));


int t_tx1, t_ty1, t_tx2, t_ty2;
t_tx1 = (x_val+tx1)*scale_val+(this->width()/2-(tx1+(tx2-tx1)/2)*scale_val);
t_ty1 = (y_val+ty1)*scale_val+(this->height()/2-(ty1+(ty2-ty1)/2)*scale_val);
t_tx2 = (x_val+tx2)*scale_val+(this->width()/2-(tx1+(tx2-tx1)/2)*scale_val);
t_ty2 = (y_val+ty2)*scale_val+(this->height()/2-(ty1+(ty2-ty1)/2)*scale_val);


triangleDraw(t_tx1, t_ty1, t_tx2, t_ty2, t_tx1, t_ty2);
triangleDraw(t_tx1, t_ty1, t_tx2, t_ty2, t_tx2, t_ty1);
painter.end();
e->accept();
}

void cDrawField::triangleDraw(double tmpx1, double tmpy1, double tmpx2, double tmpy2, double tmpx3, double tmpy3)
{
double tmpx4, tmpy4, tmpx5, tmpy5, tmpx6, tmpy6;
double sqt; // �������
double a, b, c, p; // ������� � ������������
QString count;
painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));
painter.drawLine(tmpx1, tmpy1, tmpx2, tmpy2);
painter.drawLine(tmpx2, tmpy2, tmpx3, tmpy3);
painter.drawLine(tmpx3, tmpy3, tmpx1, tmpy1);
counter++;
count = QString::number(counter);
triangle.id = counter;
triangle.x1 = tmpx1;
triangle.y1 = tmpy1;
triangle.x2 = tmpx2;
triangle.y2 = tmpy2;
triangle.x3 = tmpx3;
triangle.y3 = tmpy3;
painter.setFont(QFont("Times", 10, QFont::Normal));
painter.setPen(QPen(Qt::red, 1, Qt::SolidLine));
painter.drawText(tmpx1, tmpy1, count);
painter.drawText(tmpx2, tmpy2, count);
painter.drawText(tmpx3, tmpy3, count);
tmpx4 = (tmpx1 + tmpx2) / 2.0;
tmpy4 = (tmpy1 + tmpy2) / 2.0;
tmpx5 = (tmpx2 + tmpx3) / 2.0;
tmpy5 = (tmpy2 + tmpy3) / 2.0;
tmpx6 = (tmpx1 + tmpx3) / 2.0;
tmpy6 = (tmpy1 + tmpy3) / 2.0;
a = sqrt(((tmpx4 - tmpx5) * (tmpx4 - tmpx5)) + ((tmpy4 - tmpy5) * (tmpy4 - tmpy5)));
b = sqrt(((tmpx5 - tmpx6) * (tmpx5 - tmpx6)) + ((tmpy5 - tmpy6) * (tmpy5 - tmpy6)));
c = sqrt(((tmpx6 - tmpx4) * (tmpx6 - tmpx4)) + ((tmpy6 - tmpy4) * (tmpy6 - tmpy4)));
p = (a + b + c) / 2.0;
sqt = sqrt((p - a) * (p - B) * (p - c))/(scale_val*scale_val);
if ((sqt > ts) && (ts != 0))
{
triangleDraw(tmpx1, tmpy1, tmpx4, tmpy4, tmpx6, tmpy6);
triangleDraw(tmpx2, tmpy2, tmpx4, tmpy4, tmpx5, tmpy5);
triangleDraw(tmpx3, tmpy3, tmpx5, tmpy5, tmpx6, tmpy6);
triangleDraw(tmpx4, tmpy4, tmpx5, tmpy5, tmpx6, tmpy6);
}
}

#include "main.moc"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}


/*#include <QtGui/QApplication>
#include "help001.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Help001 w;
w.show();
return a.exec();
}
*/
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение
trdm
  опции профиля:
сообщение 2.3.2009, 23:10
Сообщение #3


Дмитрий Трошин
****

Группа: Участник
Сообщений: 575
Регистрация: 12.1.2008
Пользователь №: 68

Спасибо сказали: 21 раз(а)




Репутация:   6  


Цитата(FladeX @ 2.3.2009, 22:39) *
Не помогло...
Код не весь, взял только ключевые моменты.

private:
int [color="#FF0000"]triangle.id[/color];
double [color="#FF0000"]triangle.x1[/color];

так никто не делает.
кури страуструпа описание членов класса.

ПС. выделяй код тегами: [ code ] [ / code ] а не ... ну ты понял, что твои не работают...
ПС2. Делают так:
private:
int m_id;
double m_x1;



и это.... того....
внутренним переменным префикс "m_" присобачь,
автокомплит будет в студии лучше работать ;)
и вообще типа венгерская нотация.

http://lib.ru/CPPHB/cpptut.txt
Перейти в начало страницы
 
Быстрая цитата+Цитировать сообщение

Сообщений в этой теме
- FladeX   Массив объектов, размерность неизвестна   2.3.2009, 22:25
- - ViGOur   Неплохо было бы написать конструктор Triangle, а н...   2.3.2009, 22:32
- - FladeX   Не помогло... Код не весь, взял только ключевые мо...   2.3.2009, 22:39
|- - trdm   Цитата(FladeX @ 2.3.2009, 22:39) Не помог...   2.3.2009, 22:59
|- - trdm   Цитата(FladeX @ 2.3.2009, 22:39) Не помог...   2.3.2009, 23:10
- - Константин   почему Triangle наследуется от QWidget? это такой,...   2.3.2009, 23:03
- - kwisp   FladeX, специально код в одном файле написал или ...   2.3.2009, 23:31
- - FladeX   Константин, нет, это я туплю) trdm, я специально в...   2.3.2009, 23:49
|- - trdm   Цитата(FladeX @ 2.3.2009, 23:49) Констант...   2.3.2009, 23:56
|- - SABROG   Цитата(trdm @ 2.3.2009, 23:56) Цитата(Fla...   2.3.2009, 23:59
- - Litkevich Yuriy   Цитата(FladeX @ 3.3.2009, 2:49) trdm, я с...   3.3.2009, 0:01
- - FladeX   Новую тему не хочу открывать.. В общем, нужна плат...   3.3.2009, 14:30
- - kwisp   Цитата(FladeX @ 3.3.2009, 14:30) В общем,...   3.3.2009, 14:32
- - ViGOur   Цитата(FladeX @ 3.3.2009, 14:30) В общем,...   3.3.2009, 14:46
- - Константин   а оплату потом внесёшь в кошелёк сайта   3.3.2009, 16:22
- - Litkevich Yuriy   Цитата(Константин @ 3.3.2009, 19:22) а оп...   3.3.2009, 18:32
- - kwisp   Litkevich Yuriy, а на что деньги тратить? на пиво...   3.3.2009, 20:15
- - Litkevich Yuriy   Цитата(kwisp @ 3.3.2009, 23:15) а на что ...   4.3.2009, 5:23
- - FladeX   У меня денег столько нету... Почему выводит цифры...   4.3.2009, 21:24
- - kwisp   FladeX, привет. сразу скажу - я внимательно код н...   4.3.2009, 21:44
- - FladeX   нет, текст точно выводится как надо. Однако я доба...   4.3.2009, 22:02
- - kwisp   Цитата(FladeX @ 4.3.2009, 22:02) Задача с...   4.3.2009, 22:15
- - FladeX   Почти все не отображаются. А должны все отображать...   4.3.2009, 22:22
- - kwisp   Цитата(FladeX @ 4.3.2009, 22:22) Почти вс...   4.3.2009, 22:38
- - FladeX   Нет. Проблема в условии. Текст не выводится, а не ...   4.3.2009, 22:41
- - FladeX   >_< А за деньги поможете? Оч надо, вопрос ж...   5.3.2009, 12:59
- - kwisp   Цитата(FladeX @ 5.3.2009, 12:59) А за ден...   5.3.2009, 14:08
- - kwisp   Цитата(kwisp @ 5.3.2009, 14:08) на днях б...   6.3.2009, 0:10
- - FladeX   Задание вот: Задача стоит такая - вводим 2 координ...   6.3.2009, 0:27
- - kwisp   FladeX, выложи пожалуйста весь код.позырим что за...   6.3.2009, 9:37
- - FladeX   main.cpp#include <QApplication> #include ...   6.3.2009, 9:56
- - FladeX   Все еще актуально.   7.3.2009, 15:03
- - kwisp   Цитата(FladeX @ 7.3.2009, 15:03) Все еще ...   7.3.2009, 17:21
- - kwisp   FladeX, давай подумаем может это: Цитата1. Правил...   8.3.2009, 12:23
- - FladeX   Мне-то нормально, я на JavaScript именно так и сде...   8.3.2009, 18:20
- - kwisp   FladeX, давай пример на JavaScript свой. есть иде...   8.3.2009, 23:29
- - FladeX   js.js/* global var definition block */ /* begin */...   9.3.2009, 1:27


Быстрый ответОтветить в данную темуНачать новую тему
Теги
Нет тегов для показа


1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0




RSS Текстовая версия Сейчас: 28.3.2024, 18:43