EDIT: not currently possible, but being talked about in the C++ community.
*** I am new QT(C++) but have a C# background. Please forgive me, if this is not something done in QT.
I wanted to know if there is a way to create extension methods with QT(C++), much similar to what you can do C#?
Edit:
My objective is to reduce the nested function calls as they are. This is to avoid going to the trouble of creating a FilePath class, where the the QString method is overloaded to hide the formatting.
Here is an example of what I want to do.
I have a string that contains a system path. I use the QDir::fromNativeSeparatorsto escape the string for usage in the QFILE object.
Is there a way to create a static utilities class that would allow for the following?
QString Src = "E:/dfdf/dfdf/";
QFile file(Src.NativeSep());
Related
For a my personal C++ project, I'd like been able to encrypt plain text data using private key. In my project I make an extensive usage of Poco C++ library, and I'd like handle such feature with it.
Currently I am successfully handling a private key file in order to create Poco::Crypto::RSAKey.
std::filesystem::path keyFile = std::filesystem::path("MyFile");
Poco::SharedPtr<Poco::Crypto::RSAKey> key(new Poco::Crypto::RSAKey("", keyFile.string()));
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher* pRSACipher = factory.createCipher(*key.get());
std::string plainText("MyTextToEncrypt");
std::string encrypted = pRSACipher->encryptString(plainText, Poco::Crypto::Cipher::ENC_BASE64_NO_LF);
Having a look at official Poco documentation I found out that both Poco::Crypto::RSAKey and Poco::Crypto::ECKey are deprecated. Looking for an alternative to such deprecated classes both in Poco documentation and all around in web, I could not understand why such classes are declared as deprecated. Moreover I could not find which classes shouold replace them.
In same time, reading Poco::Crypto::CipherFactory documentation, it is not depracted using methods which get Poco::Crypto::RSAKey for method Poco::Crypto::CipherFactory::createCipher.
Please, can somebody tell me if using Poco::Crypto::RSAKey is still advisable, or another class should be used? And which one?
Thanks in advance!
I'm trying to create simple game in C++. At one point I want to have some setting, save and load from config file.
The config file should be read from the beginning, and should be accessible anywhere it needed.
So far I only see Singleton pattern as a solution.
Another way is to create an object an pass it down, but it can mess
up the current code.
I've also search and found something called Dependency Injection.
Is dependency injection useful in C++
Which design patterns can be applied to the configuration settings problem?
But I don't quite understand it, you still have to create an object in main and pass it down, right?
Singleton is quite simple, but some consider it antipattern, while pass it down the tree can mess up my current code. Is there any other Patterns?
P/S: I'm also curious how games load their setting.
I would suggest something simple as the following example, which circumvents any singleton-related or initialization order issue:
struct global_state
{
config _config;
};
struct game_state
{
global_state& _global_state;
};
int main()
{
global_state globals{load_config_from_file()};
game_state game{globals};
game.run();
}
Since _global_state is a member of game_state, it can be used in member functions without the need of explicitly passing it as a parameter:
void game_state::update_ui()
{
const float text_size = _global_state._config.get_float("text_size");
_some_text.set_size(text_size);
}
I want to implement my own simple property system (C++) similiar to one provided by Qt's Q_PROPERTY. The problem is that Qt's properties doesn't work from inside macros which I'm trying to use to add some additional functionality above properties. The aim is to be able to declare a property and automatically get access both through properties string name representation and regular methods:
MY_PROPERTY(QString, Name)
...
getObject()->setProperty("Name", "John");
...
myObject->setName("John");
QString name = myObject->getName();
I want to add all the needed functionality with a single line but the following code will not work, since MOC doesn't expand macros:
#define MY_PROPERTY(type, name)\
Q_PROPERTY(type name READ name WRITE change##name)\
\
void set##name(type param)\
{\
m_##name = param;\
DO SOMETHING
}\
Please advice any good books/articles on this topic.
most (>95%) condition, Qt Propery System is enough, if you really want to add something on yourself property when its setting or getting, you can use Qt signal/slot System to do this.
if you finally still want to do you own Property System, I think the best reference is Qt source code, isn't it?
I'm an unfortunate beginner at C++ and using the Qt GUI designer program seemed perfect for my needs, except I'm having problems trying to write out the code necessary for this. I could use the QSettings string to store local settings on the hard drive, but I personally hate it when programs do the %HOME_LOCAL%\APPS_SETTINGS bull that some do. I need to save a text file for both settings and a local\host database, within the program directory, to remember strings to read from later.
What is the line of code I need to make use of a local host text database or is there a better option? And how can I store that with the local program inside its directory?
You can use QSettings with any file, with constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 ).
To get the program directory, you can use QCoreApplication::applicationDirPath().
So, answer to your question, statement to put after creation of QApplication instance:
QSettings *settings = new QSettings(
QCoreApplication::applicationDirPath() + "/settings.ini",
QSettings::IniFormat,
qApp);
But, as noted in the comments under question, if you're making your program for general distribution, you should use the OS default. Examine all the constructors of QSettings to see what it can do. User does not often have write permission in the application directory. Note that you can also store settings to Windows registry with QSettings::NativeFormat.
I am aware of how to get a specific file from the Resources folder in cocoa, i.e. :
NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImage = [myBundle pathForResource:#"Seagull" ofType:#"jpg"];
anyways, I'd like to have a simple function that gives me the path to the Resources folder so I can use it in c++ like this.
String getResourcePath()
{
return the correct path here
}
std::ofstream theFile;
theFile.open(getResourcePath()+"test.txt");
I guess I could manually combine the main bundle name with Contents/Resources, anyways I'd like to know if there is a more robust solution!
Thanks!
If you ever wonder if an Apple-provided class has a certain method, do look up Apple's own documentation before asking the question here at SO.
In this case, the answer is this method. But let me reiterate:
If you ever wonder if an Apple-provided class has a certain method, do look up Apple's own documentation before asking the question here at SO.
And, when you do so, go over all the methods in the documentation. That way, you'll learn what kind of methods are available.