想简化代码,但是不会用容器。
ChatInterface.h:
#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
#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);}
#include "ChatInterface.h"#include <QApplication>int main(int argc,char **argv){ QApplication app(argc,argv); ChatInterface *chatInterface = new ChatInterface(); chatInterface->show(); return app.exec();}