QT界面中,点击一个按钮,会弹出小界面,并且可以在小界面上进行输入和点击按钮。请问如何实现?
展开全部
抽了点时间给你写了一个,你看看:
mainwidget.h
#ifndef MAINWIDGET_H#define MAINWIDGET_H
#include <QWidget>
#include <QPushButton>
#include "systemset.h"
class mainWidget : public QWidget
{
Q_OBJECT
public:
mainWidget(QWidget *parent = 0);
~mainWidget();
public slots:
void setWidgetShowSlot();
private:
QPushButton *m_ppbSet;
systemSet *m_psysSetWidget;
};
#endif // MAINWIDGET_H
mainwidget.cpp
#include "mainwidget.h"
mainWidget::mainWidget(QWidget *parent)
: QWidget(parent)
{
m_ppbSet = new QPushButton(this);
m_ppbSet->setText("设置");
m_ppbSet->setGeometry(145,200,80,25);
m_psysSetWidget = new systemSet();
connect(m_ppbSet,&QPushButton::clicked,this,&mainWidget::setWidgetShowSlot);
}
mainWidget::~mainWidget()
{
}
void mainWidget::setWidgetShowSlot()
{
m_psysSetWidget->show();
}
systemset.h
#ifndef SYSTEMSET_H#define SYSTEMSET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QToolButton>
class systemSet : public QWidget
{
Q_OBJECT
public:
systemSet(QWidget *parent = 0);
~systemSet();
void InitializeComponents();
void InitializeLayout();
protected:
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent *e);
virtual void mouseReleaseEvent(QMouseEvent *e);
virtual void paintEvent(QPaintEvent *e);
virtual void keyPressEvent(QKeyEvent *e);
signals:
void PostValueSig(bool Voice, int Volume,int times);
public slots:
void OkButtonPressSlot();
void CancelButtonPressSlot();
private:
QPoint m_ptDragging;
QPushButton *m_ppbOk;
QPushButton *m_ppbCancel;
QLabel *m_plbTitle;
QToolButton *m_ptbClose;
};
#endif // SYSTEMSET_H
systemset.cpp
#include "systemset.h"
#include <QPalette>
#include <QPainter>
#include <QStyle>
#include <QMouseEvent>
systemSet::systemSet(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_AcceptTouchEvents);
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint |Qt::Tool);
QPalette pal(palette());
pal.setColor(QPalette::Background, QColor(18, 118, 116));
setAutoFillBackground(true);
setPalette(pal);
setGeometry(700, 300, 380, 210);
InitializeComponents();
InitializeLayout();
connect(m_ptbClose, SIGNAL(clicked()), this, SLOT(CancelButtonPressSlot()));
connect(m_ppbOk, SIGNAL(clicked()), this, SLOT(OkButtonPressSlot()));
connect(m_ppbCancel, SIGNAL(clicked()), this, SLOT(CancelButtonPressSlot()));
}
void systemSet::InitializeComponents()
{
m_plbTitle = new QLabel("声音设置", this);
m_plbTitle->setFont(QFont("Microsoft YaHei", 12, QFont::Normal));
m_plbTitle->setStyleSheet("QLabel{color: white}");
m_ptbClose = new QToolButton(this);
m_ptbClose->setIcon(style()->standardPixmap(QStyle::SP_TitleBarCloseButton));
m_ptbClose->setStyleSheet("background-color:transparent");
m_ppbOk = new QPushButton("确定", this);
m_ppbOk->setStyleSheet("QPushButton{border-color:green;border:1px solid lightgray; background:rgb(250,250,250);}"
"QPushButton:hover{border-color:green;border:1px solid lightgray; background:rgb(227,244,255);}"
"QPushButton:pressed{border-color:green;border:1px solid lightgray; background:rgb(242,246,250)};");
m_ppbCancel = new QPushButton("取消", this);
m_ppbCancel->setStyleSheet("QPushButton{border-color:green;border:1px solid lightgray; background:rgb(250,250,250);}"
"QPushButton:hover{border-color:green;border:1px solid lightgray; background:rgb(227,244,255);}"
"QPushButton:pressed{border-color:green;border:1px solid lightgray; background:rgb(242,246,250)};");
}
void systemSet::InitializeLayout()
{
m_ptbClose->setGeometry(width() - 25, 5, 20, 20);
m_plbTitle->setGeometry(20, 0, 80, 30);
m_ppbOk->setGeometry(150, 160, 45, 25);
m_ppbCancel->setGeometry(220, 160, 45, 25);
}
systemSet::~systemSet()
{
}
void systemSet::OkButtonPressSlot()
{
close();
}
void systemSet::CancelButtonPressSlot()
{
close();
}
void systemSet::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(QColor(18, 118, 116), 2));
painter.setBrush(QColor(243, 251, 255));
painter.drawRect(20, 30, 340, 165);
}
void systemSet::mousePressEvent(QMouseEvent *e)
{
if (Qt::LeftButton == e->button())
{
m_ptDragging = e->globalPos() - frameGeometry().topLeft();
e->accept();
}
}
void systemSet::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() && Qt::LeftButton && e->y() <= 30)
{
move(e->globalPos() - m_ptDragging);
e->accept();
}
}
void systemSet::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape)
{
e->ignore();
}
}
void systemSet::mouseReleaseEvent(QMouseEvent *e)
{
}
main.cpp
#include "mainwidget.h"#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWidget w;
w.show();
return a.exec();
}
新建一个widget工程,把这些代码复制近期就好了
效果图:
点击设置后:
可以私信我,我把整个工程发给你
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询