-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
442 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
############################################################################### | ||
# Set default behavior to automatically normalize line endings. | ||
############################################################################### | ||
* text=auto | ||
|
||
############################################################################### | ||
# Set default behavior for command prompt diff. | ||
# | ||
# This is need for earlier builds of msysgit that does not have it on by | ||
# default for csharp files. | ||
# Note: This is only used by command line | ||
############################################################################### | ||
#*.cs diff=csharp | ||
|
||
############################################################################### | ||
# Set the merge driver for project and solution files | ||
# | ||
# Merging from the command prompt will add diff markers to the files if there | ||
# are conflicts (Merging from VS is not affected by the settings below, in VS | ||
# the diff markers are never inserted). Diff markers may cause the following | ||
# file extensions to fail to load in VS. An alternative would be to treat | ||
# these files as binary and thus will always conflict and require user | ||
# intervention with every merge. To do so, just uncomment the entries below | ||
############################################################################### | ||
#*.sln merge=binary | ||
#*.csproj merge=binary | ||
#*.vbproj merge=binary | ||
#*.vcxproj merge=binary | ||
#*.vcproj merge=binary | ||
#*.dbproj merge=binary | ||
#*.fsproj merge=binary | ||
#*.lsproj merge=binary | ||
#*.wixproj merge=binary | ||
#*.modelproj merge=binary | ||
#*.sqlproj merge=binary | ||
#*.wwaproj merge=binary | ||
|
||
############################################################################### | ||
# behavior for image files | ||
# | ||
# image files are treated as binary by default. | ||
############################################################################### | ||
#*.jpg binary | ||
#*.png binary | ||
#*.gif binary | ||
|
||
############################################################################### | ||
# diff behavior for common document formats | ||
# | ||
# Convert binary document formats to text before diffing them. This feature | ||
# is only available from the command line. Turn it on by uncommenting the | ||
# entries below. | ||
############################################################################### | ||
#*.doc diff=astextplain | ||
#*.DOC diff=astextplain | ||
#*.docx diff=astextplain | ||
#*.DOCX diff=astextplain | ||
#*.dot diff=astextplain | ||
#*.DOT diff=astextplain | ||
#*.pdf diff=astextplain | ||
#*.PDF diff=astextplain | ||
#*.rtf diff=astextplain | ||
#*.RTF diff=astextplain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<RCC> | ||
<qresource prefix="CallBackTest"> | ||
</qresource> | ||
</RCC> |
143 changes: 143 additions & 0 deletions
143
Project/QtGuiApplication/CallBackTest/CallBackTest.vcxproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{B12702AD-ABFB-343A-A199-8E24837244A3}</ProjectGuid> | ||
<Keyword>Qt4VSv1.0</Keyword> | ||
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<PlatformToolset>v141</PlatformToolset> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<PlatformToolset>v141</PlatformToolset> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<PropertyGroup Condition="'$(QtMsBuild)'=='' or !Exists('$(QtMsBuild)\qt.targets')"> | ||
<QtMsBuild>$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir> | ||
</PropertyGroup> | ||
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')"> | ||
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." /> | ||
</Target> | ||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.props')"> | ||
<Import Project="$(QtMsBuild)\qt.props" /> | ||
</ImportGroup> | ||
<ImportGroup Label="ExtensionSettings" /> | ||
<ImportGroup Label="Shared" /> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<MultiProcessorCompilation>true</MultiProcessorCompilation> | ||
<PreprocessorDefinitions>UNICODE;_UNICODE;WIN32;WIN64;QT_DLL;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<AdditionalIncludeDirectories>.\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||
<Optimization>Disabled</Optimization> | ||
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> | ||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> | ||
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Windows</SubSystem> | ||
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile> | ||
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
<AdditionalDependencies>qtmaind.lib;Qt5Cored.lib;Qt5Guid.lib;Qt5Widgetsd.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
</Link> | ||
<QtMoc> | ||
<OutputFile>.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</OutputFile> | ||
<ExecutionDescription>Moc'ing %(Identity)...</ExecutionDescription> | ||
<IncludePath>.\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName)\.;$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets</IncludePath> | ||
<Define>UNICODE;_UNICODE;WIN32;WIN64;QT_DLL;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB</Define> | ||
</QtMoc> | ||
<QtUic> | ||
<ExecutionDescription>Uic'ing %(Identity)...</ExecutionDescription> | ||
<OutputFile>.\GeneratedFiles\ui_%(Filename).h</OutputFile> | ||
</QtUic> | ||
<QtRcc> | ||
<ExecutionDescription>Rcc'ing %(Identity)...</ExecutionDescription> | ||
<OutputFile>.\GeneratedFiles\qrc_%(Filename).cpp</OutputFile> | ||
</QtRcc> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<MultiProcessorCompilation>true</MultiProcessorCompilation> | ||
<PreprocessorDefinitions>UNICODE;_UNICODE;WIN32;WIN64;QT_DLL;QT_NO_DEBUG;NDEBUG;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<AdditionalIncludeDirectories>.\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName);$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | ||
<DebugInformationFormat /> | ||
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> | ||
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Windows</SubSystem> | ||
<OutputFile>$(OutDir)\$(ProjectName).exe</OutputFile> | ||
<AdditionalLibraryDirectories>$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
<GenerateDebugInformation>false</GenerateDebugInformation> | ||
<AdditionalDependencies>qtmain.lib;Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
</Link> | ||
<QtMoc> | ||
<OutputFile>.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</OutputFile> | ||
<ExecutionDescription>Moc'ing %(Identity)...</ExecutionDescription> | ||
<IncludePath>.\GeneratedFiles;.;$(QTDIR)\include;.\GeneratedFiles\$(ConfigurationName)\.;$(QTDIR)\include\QtCore;$(QTDIR)\include\QtGui;$(QTDIR)\include\QtANGLE;$(QTDIR)\include\QtWidgets</IncludePath> | ||
<Define>UNICODE;_UNICODE;WIN32;WIN64;QT_DLL;QT_NO_DEBUG;NDEBUG;QT_CORE_LIB;QT_GUI_LIB;QT_WIDGETS_LIB</Define> | ||
</QtMoc> | ||
<QtUic> | ||
<ExecutionDescription>Uic'ing %(Identity)...</ExecutionDescription> | ||
<OutputFile>.\GeneratedFiles\ui_%(Filename).h</OutputFile> | ||
</QtUic> | ||
<QtRcc> | ||
<ExecutionDescription>Rcc'ing %(Identity)...</ExecutionDescription> | ||
<OutputFile>.\GeneratedFiles\qrc_%(Filename).cpp</OutputFile> | ||
</QtRcc> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="main.cpp" /> | ||
<ClCompile Include="MainWindow.cpp" /> | ||
<ClCompile Include="Square.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<QtRcc Include="CallBackTest.qrc" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<QtMoc Include="MainWindow.h" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<QtUic Include="MainWindow.ui" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<QtMoc Include="Square.h" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')"> | ||
<Import Project="$(QtMsBuild)\qt.targets" /> | ||
</ImportGroup> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
<ProjectExtensions> | ||
<VisualStudio> | ||
<UserProperties MocDir=".\GeneratedFiles\$(ConfigurationName)" UicDir=".\GeneratedFiles" RccDir=".\GeneratedFiles" lupdateOptions="" lupdateOnBuild="0" lreleaseOptions="" Qt5Version_x0020_x64="msvc2017_64" MocOptions="" /> | ||
</VisualStudio> | ||
</ProjectExtensions> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "MainWindow.h" | ||
#include "ui_MainWindow.h" | ||
|
||
QLineEdit * MainWindow::line1; | ||
QLineEdit * MainWindow::line2; | ||
|
||
MainWindow::MainWindow(QWidget *parent) | ||
: QWidget(parent) | ||
{ | ||
ui = new Ui::MainWindow(); | ||
ui->setupUi(this); | ||
|
||
// 初始化QLineEdit | ||
line1 = new QLineEdit(); | ||
line2 = new QLineEdit(); | ||
|
||
// 把两个line 放进gridLayout | ||
ui->gridLayout->addWidget(line1, 0, 1); | ||
ui->gridLayout->addWidget(line2, 0, 2); | ||
|
||
m_scene = new QGraphicsScene(); // 初始化图形场景 | ||
ui->graphicsView->setScene(m_scene); // 设置场景到 graphicsView | ||
m_scene->setSceneRect(0, 0, 300, 300); | ||
m_square = new Square(); | ||
m_square->setCallbackFunc(getPosition); // 将getPosition设置回调 接收m_square传入的数据 | ||
m_square->setPos(100, 100); | ||
m_scene->addItem(m_square); | ||
} | ||
|
||
MainWindow::~MainWindow() | ||
{ | ||
delete ui; | ||
} | ||
|
||
/** | ||
* @fn MainWindow::getPosition | ||
* @brief 回调函数接收正方形位置 写入两个lineEdit | ||
* @param QPointF point | ||
* @return void | ||
*/ | ||
void MainWindow::getPosition(QPointF point) | ||
{ | ||
line1->setText(QString::number(point.x())); | ||
line2->setText(QString::number(point.y())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include <QWidget> | ||
#include <QGraphicsScene> | ||
#include <QLineEdit> | ||
#include "Square.h" | ||
namespace Ui { class MainWindow; }; | ||
|
||
class MainWindow : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = Q_NULLPTR); | ||
~MainWindow(); | ||
|
||
private: | ||
Ui::MainWindow* ui; | ||
|
||
QGraphicsScene* m_scene; | ||
Square *m_square; // We declare the square, which will transmit the callback function | ||
static QLineEdit *line1; // We declare a static QLineEdit, which will run the callback function | ||
static QLineEdit *line2; // We declare a static QLineEdit, which will run the callback function | ||
|
||
private: | ||
// ÉùÃ÷Ò»¸ö»Øµ÷º¯Êý | ||
static void getPosition(QPointF point); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QWidget" name="MainWindow"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>MainWindow</string> | ||
</property> | ||
<layout class="QGridLayout" name="gridLayout_2"> | ||
<item row="0" column="0"> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<layout class="QGridLayout" name="gridLayout"/> | ||
</item> | ||
<item> | ||
<widget class="QGraphicsView" name="graphicsView"/> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</widget> | ||
<layoutdefault spacing="6" margin="11"/> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "Square.h" | ||
#include <windows.h> | ||
|
||
Square::Square(QObject *parent) | ||
: QObject(parent), QGraphicsItem() | ||
{ | ||
m_timer = new QTimer(); | ||
connect(m_timer, &QTimer::timeout, this, &Square::slotTimer); | ||
m_timer->start(1000 / 33); | ||
} | ||
|
||
Square::~Square() | ||
{ | ||
} | ||
|
||
void Square::setCallbackFunc(void(*func) (QPointF point)) | ||
{ | ||
m_callbackFunc = func; | ||
} | ||
|
||
QRectF Square::boundingRect() const | ||
{ | ||
return QRectF(-15, -15, 30, 30); | ||
} | ||
|
||
void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||
painter->setPen(Qt::black); | ||
painter->setBrush(Qt::green); | ||
painter->drawRect(-15, -15, 30, 30); | ||
|
||
Q_UNUSED(option); | ||
Q_UNUSED(widget); | ||
} | ||
|
||
void Square::slotTimer() | ||
{ | ||
// 根据按钮触发情况移动正方形 | ||
if (GetAsyncKeyState('A')) | ||
{ | ||
this->setX(this->x() - 2); | ||
} | ||
if (GetAsyncKeyState('D')) | ||
{ | ||
this->setX(this->x() + 2); | ||
} | ||
if (GetAsyncKeyState('W')) | ||
{ | ||
this->setY(this->y() - 2); | ||
} | ||
if (GetAsyncKeyState('S')) | ||
{ | ||
this->setY(this->y() + 2); | ||
} | ||
// 调用回调函数传递正方形位置 | ||
m_callbackFunc(this->pos()); | ||
} |
Oops, something went wrong.