首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

念简化代码,但是不会用容器

2012-07-20 
想简化代码,但是不会用容器。ChatInterface.h:C/C++ code#ifndef CHATINTERFACE_H#define CHATINTERFACE_H#

想简化代码,但是不会用容器。
ChatInterface.h:

C/C++ code
#ifndef CHATINTERFACE_H#define CHATINTERFACE_H#include <QtGui>class ChatInterface:public QToolBox{    Q_OBJECTpublic:    ChatInterface(QWidget *parent = 0,Qt::WindowFlags f = 0);    QToolButton *toolButton1_1;    QToolButton *toolButton1_2;    QToolButton *toolButton1_3;    QToolButton *toolButton1_4;    QToolButton *toolButton2_1;    QToolButton *toolButton2_2;    QToolButton *toolButton2_3;    QToolButton *toolButton3_1;    QToolButton *toolButton3_2;public:    void SetLayout();    void Communicate();public slots:    void test();};#endif // CHATINTERFACE_H


ChatInterface.cpp:
C/C++ code
#include "ChatInterface.h"ChatInterface::ChatInterface(QWidget *parent, Qt::WindowFlags f)    :QToolBox(parent,f){    SetLayout();    Communicate();}void ChatInterface::test(){    QSplitter *splitter1  = new QSplitter(Qt::Horizontal,0);    QSplitter *splitter2  = new QSplitter(Qt::Vertical,splitter1);    QTextEdit *textEdit1 = new QTextEdit(splitter1);    QTextEdit *textEdit2 = new QTextEdit(splitter2);    splitter1->show();}void ChatInterface::Communicate(){    connect(toolButton1_1,SIGNAL(clicked()),SLOT(test()));}void ChatInterface::SetLayout(){    setWindowTitle("Hi!! Mixmixi666");    toolButton1_1 = new QToolButton;    toolButton1_2 = new QToolButton;    toolButton1_3 = new QToolButton;    toolButton1_4 = new QToolButton;    toolButton2_1 = new QToolButton;    toolButton2_2 = new QToolButton;    toolButton2_3 = new QToolButton;    toolButton3_1 = new QToolButton;    toolButton3_2 = new QToolButton;    QGroupBox *myFriends = new QGroupBox();    toolButton1_1->setIcon(QPixmap("C:\\0.ico"));    toolButton1_1->setIconSize(QPixmap("C:\\0.ico").size());    toolButton1_2->setIcon(QPixmap("C:\\2.ico"));    toolButton1_2->setIconSize(QPixmap("C:\\2.ico").size());    toolButton1_3->setIcon(QPixmap("C:\\3.ico"));    toolButton1_3->setIconSize(QPixmap("C:\\3.ico").size());    toolButton1_4->setIcon(QPixmap("C:\\4.ico"));    toolButton1_4->setIconSize(QPixmap("C:\\4.ico").size());    QGroupBox *strangers = new QGroupBox;    toolButton2_1->setIcon(QPixmap("C:\\5.ico"));    toolButton2_1->setIconSize(QPixmap("C:\\5.ico").size());    toolButton2_2->setIcon(QPixmap("C:\\6.ico"));    toolButton2_2->setIconSize(QPixmap("C:\\6.ico").size());    toolButton2_3->setIcon(QPixmap("C:\\7.ico"));    toolButton2_3->setIconSize(QPixmap("C:\\7.ico").size());    QGroupBox *badpeople = new QGroupBox;    toolButton3_1->setIcon(QPixmap("C:\\8.ico"));    toolButton3_1->setIconSize(QPixmap("C:\\8.ico").size());    toolButton3_2->setIcon(QPixmap("C:\\9.ico"));    toolButton3_2->setIconSize(QPixmap("C:\\9.ico").size());    //toolButton3_2->setAutoRaise(TRUE);    QVBoxLayout *vBoxLayout1 = new QVBoxLayout(myFriends);    vBoxLayout1->addWidget(toolButton1_1);    vBoxLayout1->addWidget(toolButton1_2);    vBoxLayout1->addWidget(toolButton1_3);    vBoxLayout1->addWidget(toolButton1_4);    QVBoxLayout *vBoxLayout2 = new QVBoxLayout(strangers);    vBoxLayout2->addWidget(toolButton2_1);    vBoxLayout2->addWidget(toolButton2_2);    vBoxLayout2->addWidget(toolButton2_3);    QVBoxLayout *vBoxLayout3 = new QVBoxLayout(badpeople);    vBoxLayout3->addWidget(toolButton3_1);    vBoxLayout3->addWidget(toolButton3_2);    QString *text1 = new QString("my friends");    QString *text2 = new QString("strangers");    QString *text3 = new QString("badpeople");    addItem( ( QWidget* )myFriends,*text1);    addItem( ( QWidget* )strangers,*text2);    addItem( ( QWidget* )badpeople,*text3);} 


main.cpp
C/C++ code
#include "ChatInterface.h"#include <QApplication>int main(int argc,char **argv){    QApplication app(argc,argv);    ChatInterface *chatInterface = new ChatInterface();    chatInterface->show();        return app.exec();}

每一个按钮我都要触发事件,而事件触发的事情差不多的都是一样,我想用像数组一样,一个for循环,把它们一个一个地连接信号。。。
查了很多的资料,好像要用到什么容器什么的。。。
但是自己又不会用。。。
求各位过路人指点,或者点代码给我研究下,谢谢~~~谢谢~~~谢谢~~~谢谢~~~谢谢~~~谢谢~~~
谢谢~~~谢谢~~~谢谢~~~谢谢~~~谢谢~~~谢谢~~~
弄了一个晚上了。。。
恳请各位帮帮我。。。

[解决办法]
探讨

引用:

你代码里new了3个QGroupBox,把对应的toolbutton加进去,connect时候连QGroupBox,在对应的slot里面根据id判断是哪个按钮再具体处理

是喔,好主意。。。
但是该怎么判断ID??
connect(myFriends,SIGNAL(clicked()),SLOT(??));
……

[解决办法]
用容器的话,大概是这样的:
声明:
#include<vector>
// 因为不需要自己释放,简单实用指针即可,否则要使用 shared_ptr或者 QSharedPointer
std::vector<QToolButton *> m_buttons;

初始化:
for(int i = 0; i < 8; ++i)
{
QToolButton * btn = new QToolButton();
btn->setIcon(QPixmap(QString("c:/%1.ico").arg(i)));
m_buttons.push_back(btn);
}

事件连接:
connect(m_buttons[1], SIGNAL(clicked()), SLOT(OnButton1());
connect(m_buttons[1], SIGNAL(clicked()), SLOT(OnButton2());

热点排行