Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
How to Use QPushButton/zh: Difference between revisions
AutoSpider (talk | contribs) (Convert ExpressionEngine links) |
AutoSpider (talk | contribs) (Don't #include the module prefix) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
[[Category:Tutorial]] | [[Category:Tutorial]] | ||
'''简体中文''' [[How_to_Use_QPushButton|English]] [[How_to_Use_QPushButton_Bulgarian|Български]] [[How_to_Use_QPushButton_Spanish|Spanish]] | '''简体中文''' [[How_to_Use_QPushButton|English]] [[How_to_Use_QPushButton_Bulgarian|Български]] [[How_to_Use_QPushButton_Spanish|Spanish]] | ||
Line 30: | Line 29: | ||
=== 继承自QWidget的 === | === 继承自QWidget的 === | ||
* void customContextMenuRequested ( const QPoint & | * void customContextMenuRequested ( const QPoint & pos ) | ||
=== 继承自QObject的 === | === 继承自QObject的 === | ||
Line 68: | Line 67: | ||
#define MAINWINDOW_H | #define MAINWINDOW_H | ||
#include < | #include <QMainWindow> | ||
#include < | #include <QPushButton> | ||
namespace Ui { | namespace Ui { | ||
Line 98: | Line 97: | ||
#include "mainwindow.h" | #include "mainwindow.h" | ||
#include < | #include <QCoreApplication> | ||
MainWindow::MainWindow(QWidget *parent) | MainWindow::MainWindow(QWidget *parent) | ||
Line 134: | Line 133: | ||
#include "mainwindow.h" | #include "mainwindow.h" | ||
#include < | #include <QApplication> | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) |
Latest revision as of 13:26, 27 April 2015
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine. Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean. |
简体中文 English Български Spanish
Русский
怎样使用QPushButton?
QPushButton概览
开发人员可以使用 QPushButton 来创建和操控按钮。这个类易于使用和被定制,所以它是Qt中最有用的类之一。一般来说,按钮只显示位于其上的文字,不过它也可以同时显示一个图标。
QPushButton继承自 QAbstractButton ,后者继承自 QWidget 。
信号
继承自QAbstractButton的
- void clicked ( bool checked = false )
- void pressed ()
- void released ()
- void toggled ( bool checked )
继承自QWidget的
- void customContextMenuRequested ( const QPoint & pos )
继承自QObject的
- void destroyed ( QObject * obj = 0 )
基本用法
文本
QPushButton的文本可以在按钮被创建时设置,也可以在之后使用 setText() 方法设置。使用 text() 方法来获得按钮当前的文本。
图标
同样的,QPushButton的图标可以在按钮被创建时设置,也可以在之后使用 setIcon() 方法设置。使用 icon() 方法来获得按钮当前的图标。
位置和大小的设置
使用 setGeometry() 方法来设置按钮的位置和大小。如果只是想修改按钮的大小,使用 resize() 方法来设置。
按钮的操控
当有事件发生时,QPushButton会发出信号。要操控按钮,把确切的信号和一个信号相连接:
connect(m_button, SIGNAL (released()),this, SLOT (handleButton())); <code>
== 示例 ==
下面这个简单的代码片段演示了怎样创建和使用QPushButton。它已经在Qt Symbian Simulator上通过了测试。
你会看到,一个QPushButton的实例被创建出来。信号 '''released()''' 和槽 '''handleButton()''' 连接在一起,它们用于改变按钮的文本和大小。
=== mainwindow.h ===
- ifndef MAINWINDOW_H
- define MAINWINDOW_H
- include <QMainWindow>
- include <QPushButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0); virtual ~MainWindow();
private slots:
void handleButton();
private:
QPushButton *m_button;
};
- endif // MAINWINDOW_H
=== mainwindow.cpp ===
- include "mainwindow.h"
- include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 创建按钮 m_button = new QPushButton("My Button", this);
// 设置按钮的大小和位置
m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
// 把按钮发出的信号连接到确切的槽
connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));
}
void MainWindow::handleButton() {
// 改变按钮的文本 m_button->setText("Example");
// 改变按钮的大小
m_button->resize(100,100);
}
MainWindow::~MainWindow() {
}
=== main.cpp ===
- include "mainwindow.h"
- include <QApplication>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.showMaximized(); return app.exec();
}
参见
欢迎讨论: