C++ Qt引擎 (2)按钮相关接口、对象树的概念

5 篇文章 0 订阅
本文介绍了Qt应用程序开发中的按钮相关接口,包括QPushButton的构造函数和相关设置方法,如resize、setFixedSize、setParent等。此外,还讲解了对象树的概念,即在QT中,由QObject派生的类在程序结束时会自动释放,遵循从子对象到父对象的释放顺序。同时,文章展示了如何自定义按钮类MyButton,并在Widget窗口中添加和使用自定义按钮。
摘要由CSDN通过智能技术生成

Qt应用程序开发框架

一、按钮相关接口

  •   QPushButton 继承自 QAbstructButton 继承自 QWidget 继承自 QObject
  •   构造函数:无参构造  带参构造
  •   auto b = new QPushButton;无参构造
  •   auto b2 = new QPushButton(文本, parent);带参构造
  •  
  •   相关接口
  •   resize 重置大小
  •   setFixedSize 设置固定大小
  •   move 设置位置
  •   setParent 设置父容器
  •   setText 设置文本
  •   setFont 设置字体
  •   setStyleSheet 设置文本样式属性
  •   show 显示

 

#include "Widget.h"
#include <QApplication>//应用程序头文件

/*
 * QT编辑器快捷键
 * 注释  ctrl+/
 * 转到定义或声明切换  F2
 * 上一步、下一步  alt+← alt+→
 *
 * 运行  ctrl+R
 * 构建  ctrl+B
 * */

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//定义应用程序对象
    Widget w;//定义空窗口对象
    w.show();//调用空窗口子函数,显示窗口

    return a.exec();//调用应用程序对象 消息循环函数
}

 

二、对象树的概念

对象树:QT中的类的继承关系,有一定程度上简化了对象释放操作。由QObject派生出来的类,无需手动释放堆内存,添加到QObject或QObject派生类对象子成员时也无需手动释放
 * 程序结束时,QT会自动检测对象树的对象,释放对象前先释放子对象,从而一层一层的最终释放最上层父对象。

 

自定义按钮子类 MyButton.h

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QPushButton>
#include <QDebug>

class MyButton : public QPushButton
{
public:
    MyButton();
    MyButton(QWidget* p, QString name)
    {
        setParent(p);
        setText(name);
    }
    ~MyButton()
    {
        qDebug() << "自定义按钮被释放";
    }
};

#endif // MYBUTTON_H

MyButton.cpp

#include "Mybutton.h"

MyButton::MyButton()
{

}

 

main.cpp

#include "Widget.h"
#include <QApplication>//应用程序头文件

/*
 * QT编辑器快捷键
 * 注释  ctrl+/
 * 转到定义或声明切换  F2
 * 上一步、下一步  alt+← alt+→
 *
 * 运行  ctrl+R
 * 构建  ctrl+B
 * */

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//定义应用程序对象
    Widget w;//定义空窗口对象
    w.show();//调用空窗口子函数,显示窗口

    return a.exec();//调用应用程序对象 消息循环函数
}

 

Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>//包含 Qt空窗口头文件

class Widget : public QWidget
{
    Q_OBJECT//支持信号 和 槽

public:
    Widget(QWidget *parent = 0);//析构函数
    ~Widget();//析构函数
};

#endif // WIDGET_H

调用类Widget代码

#include "Widget.h"
#include <QPushButton>//包含按钮头文件
#include "Student.h"
#include "Mybutton.h"

//构造函数
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //记忆接口
    this->setWindowTitle("设置窗口标题");//设置窗口标题
    this->resize(400, 500);//设置窗口大小,宽、高
    this->setFixedSize(200, 200);//设置固定窗口大小,窗口不能拖拽改变大小

    QPushButton* btn = new QPushButton;
    //btn->resize(10, 10);
    btn->setParent(this);//设置父类
    btn->setText("开始");//设置文本

    //设置字体
    QFont font("华文行楷", 20, 10, 1);//创建字体对象(字体,大小,加粗,是否倾斜)
    btn->setFont(font);

    //设置CSS  hover鼠标悬浮  pressed鼠标按下  rgba
    btn->setStyleSheet("QPushButton{ background-color: red; }\
                        QPushButton:hover{ background-color: green }\
                        QPushButton:pressed{ background-color: rgba(170, 155, 221, 1)}");

    //左上角为(0, 0)为原点
    btn->move(100, 100);//设置按钮位置

    btn->show();//显示 按钮

    //自定义button
    MyButton* myBtn = new MyButton;
    myBtn->setParent(this);//设置父类为本窗口,当本窗口释放时,会释放孩子    先释放孩子,后释放父亲
    myBtn->setText("自定义按钮");
    myBtn->show();

    //对比自定义按钮的构造方法
    QPushButton* newQPushBtn = new QPushButton("系统按钮", this);
    newQPushBtn->move(100, 50);
    newQPushBtn->show();

    MyButton* mBtn = new MyButton(this, "自定义按钮2");
    mBtn->move(100, 0);
    mBtn->show();
}

//析构函数
Widget::~Widget()
{

}

 

使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值