1.标准对话框
各种基本对话框通过调用格子不同的静态函数来完成其功能:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPushButton> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QPushButton *btn1; private slots: void on_click(); }; #endif // WIDGET_H
#include "widget.h" #include <QHBoxLayout> #include <QFileDialog> #include <QMessageBox> Widget::Widget(QWidget *parent) : QWidget(parent) { btn1 = new QPushButton; btn1->setText(tr("对话框")); QHBoxLayout *layout1 = new QHBoxLayout(this); layout1->addWidget(btn1); connect(btn1, SIGNAL(clicked()),this, SLOT(on_click())); } Widget::~Widget() { } void Widget::on_click() { QString s = QFileDialog::getOpenFileName(this, "open file", "/", "c++ files(*.cpp)"); QMessageBox::information(this, "你打开的文件", s); }
运行结果:
结论:
getOpenFileName返回的是文件的全路径名称,并不会执行代开文件的操作。操作文件需要代码另外实现。
2.toolbox类
通过实现类似QQ抽屉效果的实例来介绍QToolBox类的使用
#include "widget.h" #include <QApplication> #include "drawer.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); Drawer d;//实例化 d.resize(200, 500);//调整窗口大小 d.show();//将窗口显示到桌面 return a.exec(); }
#ifndef DRAWER_H #define DRAWER_H #include <QToolBox> #include <QToolButton> class Drawer : public QToolBox { Q_OBJECT public: explicit Drawer(QWidget *parent = 0); signals: public slots: private slots: void on_click(); private: QToolButton *toolBtn1; QToolButton *toolBtn2; QToolButton *toolBtn3; QToolButton *toolBtn4; }; #endif // DRAWER_H
#include "drawer.h" #include <QGroupBox> #include <QVBoxLayout> #include <QMessageBox> Drawer::Drawer(QWidget *parent) : QToolBox(parent) { setWindowTitle(tr("QQ"));//设置窗体的标题 toolBtn1 = new QToolButton; toolBtn1->setText(tr("赵云"));//设置按钮标题 toolBtn1->setIcon(QPixmap("zy.jpg"));//设置图像 toolBtn1->setIconSize(QPixmap("zy.jpg").size());//设置按钮大小和图像一致 toolBtn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);//设置文字在图像旁边 toolBtn2 = new QToolButton; toolBtn2->setText(tr("关羽"));//设置按钮标题 toolBtn2->setIcon(QPixmap("gy.jpg"));//设置图像 toolBtn2->setIconSize(QPixmap("gy.jpg").size());//设置按钮大小和图像一致 toolBtn2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);//设置文字在图像旁边 toolBtn3 = new QToolButton; toolBtn3 = new QToolButton; toolBtn3->setText(tr("刘备"));//设置按钮标题 toolBtn3->setIcon(QPixmap("lb.jpg"));//设置图像 toolBtn3->setIconSize(QPixmap("lb.jpg").size());//设置按钮大小和图像一致 toolBtn3->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);//设置文字在图像旁边 toolBtn4 = new QToolButton; toolBtn4 = new QToolButton; toolBtn4->setText(tr("张飞"));//设置按钮标题 toolBtn4->setIcon(QPixmap("zf.jpg"));//设置图像 toolBtn4->setIconSize(QPixmap("zf.jpg").size());//设置按钮大小和图像一致 toolBtn4->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);//设置文字在图像旁边 QGroupBox *groupBox1 = new QGroupBox; QVBoxLayout *layout1 = new QVBoxLayout(groupBox1); layout1->setMargin(10);//布局中各个窗体的显示间距 layout1->setAlignment(Qt::AlignHCenter);//布局中各个窗体的显示位置 layout1->addWidget(toolBtn1); layout1->addWidget(toolBtn2); QGroupBox *groupBox2 = new QGroupBox; QVBoxLayout *layout2 = new QVBoxLayout(groupBox2); layout2->setMargin(10);//布局中各个窗体的显示间距 layout2->setAlignment(Qt::AlignHCenter);//布局中各个窗体的显示位置 layout2->addWidget(toolBtn3); layout2->addWidget(toolBtn4); this->addItem(groupBox1, tr("我的好友")); this->addItem(groupBox2, tr("陌生人")); connect(toolBtn1, SIGNAL(clicked()), this, SLOT(on_click())); } void Drawer::on_click() { QMessageBox::about(this, "发送消息", "测试"); }
时间: 2024-11-16 11:17:01