//打印子类类名集合
void printObjectChild(const QObject *obj, int spaceCount)
{
qDebug() << QString("%1%2 : %3")
.arg("", spaceCount)
.arg(obj->metaObject()->className())
.arg(obj->objectName());
QObjectList childs = obj->children();
foreach (QObject *child, childs) {
printObjectChild(child, spaceCount + 2);
}
}
//拿到对话框进行设置和美化
QFileDialog *fileDialog = new QFileDialog(this);
fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
QLabel *lookinLabel = fileDialog->findChild<QLabel*>("lookInLabel");
lookinLabel->setText(QString::fromLocal8Bit("文件目录:"));
lookinLabel->setStyleSheet("color:red;");
//设置日期框默认值为空
QLineEdit *edit = ui->dateEdit->findChild<QLineEdit *>("qt_spinbox_lineedit");
if (!edit->text().isEmpty()) {
edit->clear();
}
QFileDialog *fileDialog = new QFileDialog(this);
//不设置此属性根本查找不到任何子元素,因为默认采用的系统对话框
fileDialog->setOption(QFileDialog::DontUseNativeDialog, true);
qDebug() << fileDialog->findChildren<QLabel *>();
//打印输出 QLabel(0x17e2ff68, name="lookInLabel"), QLabel(0x17e35f88, name="fileNameLabel"), QLabel(0x17e35e68, name="fileTypeLabel")
/**
* @brief $name$
* @param $param$
* @author feiyangqingyun
* @date $date$
*/
$ret$ $name$($param$)
{
$$
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public: MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private:
void test_fun();
private slots:
void test_slot();
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//早期写法,通用Qt所有版本,只支持定义了slots关键字的函数
//connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(test_fun()));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(test_slot()));
//新写法,支持Qt5及后期所有版本,支持所有函数,无需定义slots关键字也行
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::test_fun);
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::test_slot);
//另类写法,支持lambda表达式,直接执行代码
connect(ui->pushButton, &QPushButton::clicked, [this] {test_fun();});
connect(ui->pushButton, &QPushButton::clicked, [this] {
qDebug() << "hello lambda";
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::test_fun()
{
qDebug() << "test_fun";
}
void MainWindow::test_slot()
{
qDebug() << "test_slot";
}
Qt开发经验开源主页(持续更新):
原文:https://www.cnblogs.com/feiyangqingyun/p/14696240.html