Logo    
KDWinUtils
Helper library for MFC to Qt migration
Loading...
Searching...
No Matches
ktext.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of KDWinutils, KDAB's MFC to Qt migration tool.
4**
5** Copyright (C) 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6** All rights reserved.
7**
8** This file is intended solely for use by the migration tools and services
9** provided by Klarälvdalens Datakonsult AB.
10**
11** Any other use or distribution of this software that is not otherwise agreed
12** upon in writing and signed by an authorized representative of Klarälvdalens
13** Datakonsult AB, KDAB (USA) LLC, KDAB (Deutschland) GmbH & Co. K.G.,
14** KDAB (France) SAS, KDAB (UK), Ltd., or any future subsidiary of Klarälvdalens
15** Datakonsult AB is hereby prohibited.
16**
17** Contact info@kdab.com if any conditions stated above are unclear to you.
18**
19****************************************************************************/
20
21#pragma once
22
23#include "../corelib/kstring.h"
24
25class QWidget;
26class QValidator;
27
29
40{
41public:
43 explicit KText(const wchar_t *text);
44 explicit KText(const char *text);
45 explicit KText(QString text);
46 KText(const KText &other) = delete;
49
51 QWidget *widget() const;
52
61 void setWidget(QWidget *label);
62
64 void setValidator(QValidator *validator);
65
66 // Explicit conversion methods
67 QString toQString() const;
68 std::string toStdString() const;
69 std::wstring toStdWString() const;
70
71 // Implicit conversion methods
72 operator QString() const;
73 operator std::string() const;
74 operator std::wstring() const;
75
76 // Assignment operators
77 KText &operator=(const wchar_t *text);
78 KText &operator=(const char *text);
79 KText &operator=(const QString &text);
80 KText &operator=(const KText &other) = delete;
81
82#ifdef KDWINUTILS_WITHMFC
83 // CString specific methods, available only on Windows with MFC set
84 explicit KText(const CString &text);
85 CString toCString() const;
86 operator CString() const;
87 KText &operator=(const CString &text);
88#endif // KDWINUTILS_WITHMFC
89
98 [[deprecated("Use explicit conversion methods")]] const char *constData() const;
99
100 // MFC API from here on
102
105 template <typename T, typename... Ts>
106 KText &Format(T &&format, Ts... args)
107 {
108 setValue(KString::FormatString(format, args...));
109 return *this;
110 }
111
113 int Compare(const QString &other) const;
114
115private:
116 QString value() const;
117 void setValue(const QString &text);
118
119private:
120 QString m_string; // used only for the initialization, then move to the widget
121 QWidget *m_widget = nullptr;
122};
123
static QString FormatString(T &&format, Ts... args)
Creates a QString by passing a format string and arguments, like CString::Format.
Definition kstring.h:374
Class used as member for DDX_Text data exchange.
Definition ktext.h:40
QWidget * widget() const
Returns the widget associated with this KText.
void setValidator(QValidator *validator)
Sets a validator to the widget associated with this KText.
KText & operator=(const QString &text)
KText & Format(T &&format, Ts... args)
Writes formatted data to the widget in the same way that sprintf_s formats data into a C-style charac...
Definition ktext.h:106
void setWidget(QWidget *label)
Sets the widget associated with this KText.
KText & operator=(const KText &other)=delete
KText(QString text)
const char * constData() const
Converts to a const char * implicitly.
std::wstring toStdWString() const
int Compare(const QString &other) const
Compares two strings (case-sensitive).
KText & operator=(const CString &text)
KText & operator=(const char *text)
KText(const KText &other)=delete
KText(const wchar_t *text)
KText & operator=(const wchar_t *text)
CString toCString() const
std::string toStdString() const
QString toQString() const
KText(const char *text)
KText(const CString &text)
#define KDWINUTILS_EXPORT
Definition kdwinutils_export.h:31
#define KDWINUTILS_BEGIN_NAMESPACE
Definition kdwinutils_global.h:27
#define KDWINUTILS_END_NAMESPACE
Definition kdwinutils_global.h:28