如下图:
主窗口mainwindow包含左(QFrame)右(QTextEdit)两个分割窗口,左边窗口又包含上(QFrame)下(QWidget)两个窗口。左边上面窗口有4个按钮ToolBar,现在点击任意一个按钮,让主窗口mainwindow弹出一个QMessageBox,提示是哪一个按钮点击了。可以添加一个继承于QObject的类qpp来通过信号槽,来传递消息。
1.qpp.h
#ifndef APP_H
#define APP_H
#include <QObject>
class app : public QObject
{
Q_OBJECT
public:
app();
~app();
public slots:
void testSlot(int);
signals:
void testSignal(int);
};
#endif // APP_H
2.app.cpp添加一个槽
void app::testSlot(int nbtn)
{
emit testSignal(nbtn);
}
3.myframe.h
#ifndef MYFRAME_H
#define MYFRAME_H
#include <QFrame>
class QToolButton;
class QLabel;
class myframe : public QFrame
{
Q_OBJECT
public:
myframe();
~myframe();
private:
QToolButton *m_upButton;
QToolButton *m_downButton;
QToolButton *m_rightButton;
QToolButton *m_leftButton;
QWidget *m_wnd;
QFrame *m_bar;
QLabel *m_label;
private slots:
void btnClicked();
signals:
void frameSignal(int);
};
#endif // MYFRAME_H
4.myframe.cpp
#include "myframe.h"
#include <qtoolbutton.h>
#include <QVBoxLayout>
#include <qlabel.h>
#include "app.h"
extern app *g_app;
myframe::myframe()
: QFrame()
{
m_wnd=new QWidget(this);
m_bar=new QFrame(this);
m_bar->setMaximumHeight(20);
m_bar->setStyleSheet(tr("color: rgb(255, 0, 0);\
background-color: qlineargradient(spread:pad,\
x1:0, y1:0, x2:0, y2:1,\
stop:0 rgba(221, 220, 218, 255),\
stop:1 rgba(254, 254, 254, 255));"));
m_bar->setFrameShape(QFrame::Box);
QVBoxLayout *layout=new QVBoxLayout(this);
layout->addWidget(m_bar);
layout->addWidget(m_wnd);
m_upButton=new QToolButton(m_bar);
m_upButton->setIcon(QIcon(tr(".\\icon\\up.png")));
m_downButton=new QToolButton(m_bar);
m_downButton->setIcon(QIcon(tr(".\\icon\\down.png")));
m_rightButton=new QToolButton(m_bar);
m_rightButton->setIcon(QIcon(tr(".\\icon\\right.png")));
m_leftButton=new QToolButton(m_bar);
m_leftButton->setIcon(QIcon(tr(".\\icon\\left.png")));
QHBoxLayout* layoutForBar = new QHBoxLayout(m_bar);
layoutForBar->setContentsMargins(QMargins(1,1,1,1));//left,up,right,down
layoutForBar->setSpacing(0); ///间隔
layoutForBar->addWidget(m_upButton);
layoutForBar->addWidget(m_downButton);
layoutForBar->addWidget(m_rightButton);
layoutForBar->addWidget(m_leftButton);
layoutForBar->addStretch(); ///空白
connect(m_upButton,SIGNAL(clicked()),this,SLOT(btnClicked()));
connect(m_downButton,SIGNAL(clicked()),this,SLOT(btnClicked()));
connect(m_rightButton,SIGNAL(clicked()),this,SLOT(btnClicked()));
connect(m_leftButton,SIGNAL(clicked()),this,SLOT(btnClicked()));
connect(this,SIGNAL(frameSignal(int)),g_app,SLOT(testSlot(int)));
}
myframe::~myframe()
{
}
void myframe::btnClicked()
{
int nbtn=-1;
QObject *btn=sender();
if(btn==m_upButton)
emit frameSignal(0);
else if(btn==m_downButton)
emit frameSignal(1);
else if(btn==m_rightButton)
emit frameSignal(2);
else if(btn==m_leftButton)
emit frameSignal(3);
}
5.mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include "ui_mainwindow.h"
#include "mythread.h"
#include "myframe.h"
class QFrame;
class QDialog;
class QTextEdit;
class QSplitter;
class mainwindow : public QMainWindow
{
Q_OBJECT
public:
mainwindow(QWidget *parent = 0, Qt::WFlags flags = 0);
~mainwindow();
protected:
void closeEvent(QCloseEvent *event);
private:
Ui::mainwindowClass ui;
QTextEdit *editor1;
QSplitter *splitter;
myframe *frame1;
public slots:
void threadStart();
void threadStop();
void mainWndSlot(int);
private:
myThread thread;
};
#endif // MAINWINDOW_H
6.mainwindow.cpp
#include "mainwindow.h"
#include <qDebug>
#include <qrect.h>
#include <qframe.h>
#include <QSplitter>
#include <qtextedit.h>
#include <qmessagebox.h>
#include <QCloseEvent>
#include <qdialog.h>
#include "app.h"
extern app *g_app;
mainwindow::mainwindow(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
frame1=new myframe;
editor1=new QTextEdit(tr("just a test"));
splitter=new QSplitter(Qt::Horizontal,this); //定义一个切分窗口
splitter->addWidget(frame1); // //将文件控件加入到切分框
splitter->addWidget(editor1);
setCentralWidget(splitter);
connect(ui.action_new,SIGNAL(triggered()),this,SLOT(threadStart()));
connect(ui.action_open,SIGNAL(triggered()),this,SLOT(threadStop()));
connect(ui.action_exit,SIGNAL(triggered()),this,SLOT(close()));
connect(g_app,SIGNAL(testSignal(int)),this,SLOT(mainWndSlot(int)));
}
mainwindow::~mainwindow()
{
}
void mainwindow::mainWndSlot(int nbtn)
{
QString str=QString("it is the %1 button").arg(nbtn);
QMessageBox::about(this,tr("ok"),str);
}
void mainwindow::threadStart()
{
thread.start();
}
void mainwindow::threadStop()
{
if(thread.isRunning())
thread.stop();
}
void mainwindow::closeEvent(QCloseEvent *event)
{
thread.stop();
thread.wait();
event->accept();
}
最后main.cpp
#include "mainwindow.h"
#include "app.h"
#include <QtGui/QApplication>
app *g_app;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
app myApp;
g_app=&myApp;
mainwindow w;
w.show();
a.exec();
return 1;
}
太困了,睡觉