Qt 기초 학습(5)-----서랍 효과의 QToolBox

2615 단어 layoutdialogqt
//mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
class QToolBox;
class MyDialog : public QDialog
{
  Q_OBJECT
public:
  explicit MyDialog(QWidget *parent = 0);
signals:
public slots:
private:
  QToolBox *toolBox;
};
#endif // MYDIALOG_H

 
 
//mydialog.cpp
#include "mydialog.h"
#include <QtGui/QToolBox>
#include <QtGui/QWidget>
#include <QPushButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
MyDialog::MyDialog(QWidget *parent) :
  QDialog(parent)
{
  // QToolBox
  toolBox = new QToolBox(this);

  // QWidget  QToolBox 
  QWidget *widget = new QWidget();
  QIcon iconHahaya(":/new/ico/images/hahaya.ico");
  QPushButton *buttonHahaya = new QPushButton(iconHahaya, "hahaya");
  QIcon iconZhangSan(":/new/ico/images/zhangsan.ico");
  QPushButton *buttonZhangSan = new QPushButton(iconZhangSan, "zhangsan");
  QVBoxLayout *layout = new QVBoxLayout();
  layout->addWidget(buttonHahaya);
  layout->addWidget(buttonZhangSan);
  widget->setLayout(layout);

  // QPushButton  QToolBox 
  QIcon iconLiSi(":/new/ico/images/lisi.ico");
  QPushButton *buttonStrange = new QPushButton(iconLiSi, "lisi");

  // QToolBox   Friend
  toolBox->addItem(widget, "Friend");
  QIcon iconFriend(":/new/ico/images/friend.ico");
  // 
  toolBox->setItemIcon(0, iconFriend);

  // QToolBox   Strange
  toolBox->addItem(buttonStrange, "Strange");
  QIcon iconStrange(":/new/ico/images/strange.ico");
  toolBox->setItemIcon(1, iconStrange);

   // 
  toolBox->layout()->setSpacing(0);


  QHBoxLayout *hLayout = new QHBoxLayout();
  hLayout->addWidget(toolBox);
  hLayout->setMargin(0);

  this->setLayout(hLayout);
  this->resize(100, 200);
  this->setWindowTitle("QToolBoxDemo");
}

 
 
//main.cpp
#include <QtGui/QApplication>
#include "mydialog.h"
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MyDialog dialog;
  dialog.show();
  return a.exec();
}

 
프로그램 실행 캡처:

좋은 웹페이지 즐겨찾기