Дело в том,что я хочу создать функцию, у которой в качестве входного параметра был бы некий объект или указатель на объект, вместо которого, вызывая эту функцию можно было бы посылать в нее к примеру кнопку, или простой виджет, или же поле. Давайте я на небольшом примере обрисую вам, то что я хочу сделать:
// ----- main.cpp ----- #include "myclass.h" #include <QTextCodec>
int main(int argc,char *argv[]) { QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"""));
QApplication app(argc,argv); MyClass mycl; mycl.show(); return app.exec(); }
//------myclass.h--------- #ifndef MYCLASS_H #define MYCLASS_H
#include <QtGui> #include <QDialog>
class MyClass : public QDialog{ Q_OBJECT
public: MyClass(QWidget *parent=0); void show_wgt(TypePar *parameter, x, y, w, h);
public slots: void set_showedWidget();
private: QLineEdit *lineName; QLabel *nameObject; QPushButton *showWidget; QPushButton *close;
int size_w; int size_h; };
#endif // MYCLASS_H
// ---------myclass.cpp------- #include "myclass.h"
MyClass::MyClass(QWidget *parent) :QDialog(parent){
size_w = 400; size_h = 400; setFixedSize(size_w, size_h); setWindowTitle("Создание разных типов виджетов");
int x, y, w, h;
x = 40; y = 8; w = 100; h = 25; nameObject = new QLabel(this); nameObject->setText("gdggb"); nameObject->setGeometry(x, y, w, h);
x = 110; y = 45; w = 90; h = 25; lineName = new QLineEdit(this); lineName->setText(""); lineName->setGeometry(x, y, w, h);
x = 210; y = 10; w = 90; h = 28; showWidget = new QPushButton(this); showWidget->setText("Показать"); showWidget->setGeometry(x, y, w, h); connect(showWidget, SIGNAL(clicked()), this, SLOT(set_showedWidget()));
x = 210; y = 45; w = 90; h = 28; close = new QPushButton(this); close->setText("Выйти"); close->setGeometry(x, y, w, h); connect(close, SIGNAL(clicked()), this, SLOT(close())); }
void MyClass::set_showedWidget(){ int x, y, w, h;
x = 20; y = 45; w = 70; h = 25;
if(lineName->text() == "QWidget"){ nameObject->setText("QWidget"); QWidget *wgt1 = new QWidget(this); show_wgt(wgt1, x, y, w, h); } else if(lineName->text() == "QLabel"){ nameObject->setText("QLabel"); QLabel *wgt2 = new QLabel(this); show_wgt(wgt2, x, y, w, h);
} else if(lineName->text() == "QPushButton"){ nameObject->setText("QPushButton"); QPushButton *wgt3 = new QPushButton(this); show_wgt(wgt3, x, y, w, h); } else nameObject->setText("не тот тип"); }
void MyClass::show_wgt(TypePar *parameter, x, y, w, h){ if(parameter->metaObject == "QWidget") parameter->setStyleSheet("QWidget {background-color: red}"); if(parameter->metaObject == "QLabel") parameter->setText(lineName->text()); if(parameter->metaObject == "QPushButton") parameter->setText(lineName->text()); parameter->setGeometry(x, y, w, h); parameter->show(); }
Проблема состоит в том, что я не знаю что поставить вместо TypePar *parameter в функции show_wgt(TypePar *parameter, x, y, w, h), для того, чтобы этот пример заработал.
|