初识Qt-无法解析的外部符号

跟着教程学习QT, 第一个对象树就出现bug,自己建立一个QPushButton的类,

复制代码
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QPushButton>
class MyPushButton:public QPushButton
{    Q_OBJECT
public:   
 MyPushButton(QWidget *parent);   
 ~MyPushButton();
};
#endif // MYPUSHBUTTON_H
复制代码
#include "mypushbutton.h"
#include <QDebug>
MyPushButton::MyPushButton(QWidget *parent):QPushButton(parent)
{    qDebug()<<"HELLO WORLD";}
MyPushButton::~MyPushButton()
{    
qDebug()<<"BYE WORLD";
}

建立好类之后,在Widget.cpp中添加按钮:

MyPushButton *btn2=new MyPushButton(this);

btn2->setParent(this);

btn2->setText("MY Button");

btn2->move(200,100);

结果一直提示:

MyPushButton是一个无法解析的外部符号!

解决方案:

打开.pro文件,

复制代码
QT       += core gui后面添加QPushButton的父类,widgets,也就是

QT += core gui widgets,qmake一下,再运行,成功!

相关推荐
Sakuyu434683 分钟前
C语言基础--基本数据类型
c语言·开发语言
mengzhi啊3 分钟前
串口数据监控软件开发总结
qt
在坚持一下我可没意见5 分钟前
Python 修仙修炼录 05:循环神通,省去无用苦修
开发语言·python·面试·入门·循环·复习
小短腿的代码世界15 分钟前
Qwt实时FFT频谱分析深度解析:从信号采集到可视化渲染的完整架构设计
前端·qt·架构·交互
Hua-Jay17 分钟前
OpenCV联合C++/Qt 学习笔记(二十)----Harri角点检测、Shi-Tomas角点检测及亚像素级别角点位置优化
c++·笔记·qt·opencv·学习·计算机视觉
techdashen23 分钟前
Rust 社区在 4 月做了什么:项目管理月报解读
开发语言·rust·mfc
十五年专注C++开发24 分钟前
QFluentKit: 一个基于 Qt Widgets 的 Fluent Design 风格 UI 组件库
开发语言·c++·qt·ui·qfluentkit
lly20240626 分钟前
PHP JSON 使用指南
开发语言
沐知全栈开发31 分钟前
jQuery 尺寸
开发语言
Byte Wizard32 分钟前
C语言指针深入浅出5
c语言·开发语言