初识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一下,再运行,成功!

相关推荐
吃好睡好便好5 小时前
MATLAB中图像的读取、写入和显示
开发语言·图像处理·学习·计算机视觉·matlab
yaoxin5211235 小时前
476. Java 反射 - 调用方法
java·开发语言
△曉風殘月〆5 小时前
如何在Linux中安装Qt开发环境
linux·运维·qt
猫头虎6 小时前
什么是ZCode for GLM-5.2?
开发语言·人工智能·python·科技·算法·ai编程·ai写作
bksczm7 小时前
Linux之日志和线程池、内存池
java·开发语言
笨蛋不要掉眼泪7 小时前
Java虚拟机:对象复活、引用强度与Stop-The-World
java·开发语言·jvm
code_pgf7 小时前
C/C++ 常用容器功能汇总
c语言·开发语言·c++
布朗克1687 小时前
Go 入门到精通-33-unsafe 与 CGO
开发语言·后端·golang·unsafe·cgo
个 人 练 习 生7 小时前
strcmp与strstr函数的模拟实现
c语言·开发语言·经验分享·学习·程序人生
157092511347 小时前
【无标题】
开发语言·python·算法