Skip to content

C++ Gurus

The forum for all discussions in C++ land.
1.3k Topics 8.5k Posts
  • This topic is deleted!

    Unsolved
    2
    0 Votes
    2 Posts
    17 Views
  • Add to mdiArea using C++ code and QTDesigner

    Solved
    1
    0 Votes
    1 Posts
    197 Views
    No one has replied
  • Process Protection

    Unsolved
    12
    0 Votes
    12 Posts
    2k Views
    TomZT
    @Chris-Kawa said in Process Protection: If what you're saying was universal the entire gaming industry wouldn't exist and wouldn't be the biggest entertainment medium in the world as it is. I'd like to point out that there is no conflict in that world and what I wrote. See this description; https://www.theverge.com/2021/9/23/22690670/epic-eac-anti-cheat-linux-valve-steam-deck-support-games EasyAntiCheat is a company that doesn't focus on process protection in the way that OP asked. For starters it requires kernel support.
  • Errors when some static libs are combined into one single dynamic lib.

    Unsolved
    7
    0 Votes
    7 Posts
    1k Views
    JoeCFDJ
    @Chris-Kawa Unluckily, not, Chris. A, B and X are downloaded. If I can rebuild A and B, I will be able to build A, B and X to FOO easily. One way out may be to exclude X from the build with '-Wl,--exclude-libs, X', but break X and add its obj files to FOO while keeping A and B
  • Dynamic Programming Challenge: Longest Increasing Subsequence

    Unsolved
    2
    0 Votes
    2 Posts
    619 Views
    JonBJ
    @Sachin-Bhatt I had to look up what "dynamic programming" is! They like their labels for things, don't they? Am I missing something, or is this real easy? You start by setting a variable for the longest subsequence to 0. You count from left to right while the numbers increase. So for 10, 22 and then 9 the longest so far is 2 (10, 22), terminated by 9 (because it is less than the preceding 22). You note that 2 is the longest so far, because it is more than 0, and so update the count variable. You know that you can resume looking from the 9. You encounter 9, 33 and then 21, 50, both terminated by smaller numbers, and those are also of length 2, no better then before. Finally you meet 41, 60, 80, terminated by end of input, that is length 3, and so is the highest. So that is what you return....
  • Cross platform compatible GUI application with C++ core logic

    Unsolved
    3
    0 Votes
    3 Posts
    495 Views
    TomZT
    @prasad2023 said in Cross platform compatible GUI application with C++ core logic: Are qt gui and c++ backend are right technologies to develop such application? Obviously, you will get a "yes" here. There is a very large number of applications written using Qt that are similar to what you describe. With "similar" I don't specifically mean trading applications, I mean professional desktop applications that use high-volumes of data and aim at a great user interface. @prasad2023 said in Cross platform compatible GUI application with C++ core logic: afaik, qt apis are wrappers around native os apis. will it impact performance? This is not a good description. Qt is a toolkit and covers a LOT of ground and provides a very large number of features. Any other toolkits, including the ones from Windows or Mac will basically have the same approach that Qt has for most of these features. Therefore it won't be slower than those. The term "wrapped" I have mostly seen used for widgets. The point there is that a Qt app can look identical to a native Windows or Mac application. If you so choose. I don't think you need to worry about that being slow. @prasad2023 said in Cross platform compatible GUI application with C++ core logic: is there any alternative for c++ for this requirement. I don't think there is anything out there that will be good enough for such an application.
  • passing Q_GADGET object as argument

    Unsolved
    7
    0 Votes
    7 Posts
    751 Views
    mzimmersM
    @Axel-Spoerl no, not at all. Maybe some code will help. I have a struct: struct Equipment { Q_GADGET QML_STRUCTURED_VALUE QML_VALUE_TYPE(equipment) ... and I maintain a list of these in a model. In my QML, I have a ListView that accesses the model, and furnishes the contents to a delegate that eventually gets down to: equipmentModel.processSpaceAssignment(vspInModel, spaceUuid, checked) where vspInModel is an instance of a subclass of Equipment. There's some rather complex work that needs to be done, so I didn't want to try to implement it in JS. The C++ function signature is: bool EquipmentModel::processSpaceAssignment(Equipment equipment, QUuid spaceUuid, bool add) So, my original question was, is this equipment/vsp object passed by value in this case?
  • single function to accept different parameter types

    Solved
    23
    0 Votes
    23 Posts
    4k Views
    mzimmersM
    One of my co-workers came up with this workaround: // would entail one of these routines for each subclass, // but they would all do the exact same thing. void EquipmentModel::sendPatchRequest(const Vsp &equipment) { sendBaseRequest(equipment); } void EquipmentModel::sendBaseRequest(const Equipment &equipment) { if (m_qnrPatch == nullptr) { int i = getIndex(equipment.m_uuid); if (i == NOT_IN_LIST) { continue; } Equipment &listEntry = *(*m_list)[i]; equipment.addPatchFields(listEntry, qjo, rolesToKeys); // goes to subclass function. ... It works. Unless someone comes up with a better idea, I'll close out this topic. Thanks for all the suggestions...
  • Understanding the Code: Differentiating Between Process and Program in C++

    Unsolved
    4
    0 Votes
    4 Posts
    666 Views
    mzimmersM
    @Sachin-Bhatt said in Understanding the Code: Differentiating Between Process and Program in C++: Can you explain how this C++ code represents both a process and a program? To add to what the others said, code is never a process. Source code like yours is built (compiled and linked) into a program (something that can be run). A process is something the OS furnishes for your program is built. It might help to think of it this way: if all computers in the world ran the same OS, you could build your program, and distribute to millions of people everywhere. Every time they run it, they create a process on their system, but it's the same program (the bits in the executable file you distribute are identical). Hope this helps a little...
  • trying to understand smart pointers...

    Solved
    30
    0 Votes
    30 Posts
    5k Views
    mzimmersM
    @Chris-Kawa if I make pEquipment local to this function, then I can't use it in my insertRows() call. That's why I made it a member of the class. I can still use your technique of accessing list elements with a conventional pointer, though...thoughts? Based on what I've shown about my app, should I even be using unique_ptrs or shared_ptrs in my list/vector?
  • member initialization in definition and c'tor: redundant?

    Solved
    4
    0 Votes
    4 Posts
    443 Views
    S
    @JoeCFD said in member initialization in definition and c'tor: redundant?: the following initialization is redundant. And it will eventually get out of sync. This is only true as long as you only have this one constructor (or only copy/move constructors in addition). If the constructor does nothing else than just initialize the member, you can ditch the constructor all together. This would then even allow (with C++20) to use aggregate initialization: https://en.cppreference.com/w/cpp/language/aggregate_initialization (which would be better than default parameters in a constructor as some members could be skipped and it would be more expressive).
  • How to retrieve / match - at least next "pair"? (regular expression )

    Unsolved
    5
    0 Votes
    5 Posts
    496 Views
    JonBJ
    @AnneRanch I have written the complete example code, giving what you want, at https://forum.qt.io/topic/152045/qt-regular-expresssion-example-how-to-use-it/10
  • How to pass regular expresion to QString.contains (reg expression) ??

    Unsolved
    2
    0 Votes
    2 Posts
    327 Views
    JonBJ
    @AnneRanch if(inString.contains(^[ -~]+$ )) This will not compile as you are not passing it either a QString or a QRegularExpression. You will get a syntax error. if(inString.contains("[ -~]+")) This will compile. But because you don't pass any regular expression, only a plain string, it will look to see whether that literal string appears in your test string, which it won't. You meant: if(inString.contains(QRegularExpression("[ -~]+"))) which will tell you whether your string contains one or more ASCII characters. That would be true if your string contains a mixture of ASCII and non-ASCII characters. If you want to "fail" on any non-ASCII characters (i.e. only succeed on all ASCII characters) you want something like: if (QRegularExpression("^[ -~]+$").match(inString).hasMatch())
  • how to create multi client with single server

    Unsolved
    2
    0 Votes
    2 Posts
    370 Views
    JonBJ
    @Divij08 Client/server in what sense? Network service like TCP? Have you looked at Fortune Server example?
  • QWaitCondition deadline misunderstanding

    Solved
    3
    0 Votes
    3 Posts
    518 Views
    Q
    @Christian-Ehrlicher said in QWaitCondition deadline misunderstanding: You use the wrong setter, see QDeadlineTimer::setDeadline(): Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point, counted in milliseconds since the reference clock. I think you want QDeadlineTimer deadline(1000); or QDeadlineTimer::setRemainingTime(). Yes, everything is as you said, thank you.
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    927 Views
    No one has replied
  • Mutex, please explain what's wrong

    Unsolved
    9
    0 Votes
    9 Posts
    1k Views
    Q
    @JonB said in Mutex, please explain what's wrong: If this is your precise objective I think you cannot achieve this with a single mutex. It seems to me you have 3 alternatives: Use semaphores. I see this is the "accepted" solution at How Thanx, Semaphors are on the list of my styding. Also I have fully resolved the problem by numerating threads and blocking "last processed thread" to work twice: #include <QCoreApplication> #include <QDebug> #include <QMutex> #include <QThread> #include <QObject> #include <QDebug> #include <QMutexLocker> QMutex mutex; int count = 0; int last_thread_num = 0; //================= class thread1_tst : public QThread { Q_OBJECT public: int _thread_num; int& _last_thread_num; explicit thread1_tst(int thead_num, int& last_thread_num, QObject *parent = nullptr); void run(); void my_work(); }; //------------------------------ thread1_tst::thread1_tst(int thread_num, int& last_thread_num, QObject *parent) : QThread{parent}, _thread_num(thread_num), _last_thread_num(last_thread_num) {} //------------------------------ void thread1_tst::run() { while(1) { if(_thread_num != _last_thread_num) my_work(); else msleep(1); } } //------------------------------ void thread1_tst::my_work() { { QMutexLocker locker(&mutex); for(int i=0; i < 5; i++) qDebug() << "thread 1 out: count=" << count++ << " (" << i << ")"; qDebug() << "----end 1 -----"; } msleep(1000); last_thread_num = _thread_num; } //================= class thread2_tst : public QThread { Q_OBJECT public: int _thread_num; int& _last_thread_num; explicit thread2_tst(int thead_num, int& busy_fl, QObject *parent = nullptr); void run(); void my_work(); }; thread2_tst::thread2_tst(int thread_num, int& last_thread_num, QObject *parent) : QThread{parent}, _thread_num(thread_num), _last_thread_num(last_thread_num) {} void thread2_tst::run() { while(1) { if(_thread_num != _last_thread_num) my_work(); else msleep(1); } } //------------------------------ void thread2_tst::my_work() { { QMutexLocker locker(&mutex); for(int i=0; i < 5; i++) qDebug() << "thread 2 out: count=" << count++ << " (" << i << ")"; qDebug() << "----end 2 -----"; } msleep(1000); last_thread_num = _thread_num; } //============================= thread1_tst thread1(0, last_thread_num); //make threads enumerated thread2_tst thread2(1, last_thread_num); int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); thread1.start(); thread2.start(); return a.exec(); } #include "waitconditions.moc"
  • Nested Control Structures in C++: Finding Prime Numbers in a Range

    Unsolved
    3
    0 Votes
    3 Posts
    797 Views
    JonBJ
    @J-Hilk One difference to be aware of is that Eratosthenes or similar uses storage (primes[]) for its computation where the OP's does not. This might become costly if upper bound is large? I refused to use this approach on my computations of primes into the 100s of billions for this reason! I picked a compromise where I store all the primes as I calculate them for future re-use, but not maintain an array holding true/false for every number in the range. @Sachin-Bhatt for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } I do not recall coming across this approach/algorithm/equation. Can you provide a reference link, please? I wonder how it compares against my way of storing all the previous primes and doing a MOD using just them? You visit every number in steps of 6 and do 2 MODs. That is still a lot of numbers to visit if n is into the trillions?
  • QMessage box in console app slot

    Solved
    3
    0 Votes
    3 Posts
    359 Views
    Q
    @J-Hilk said in QMessage box in console app slot: @qAlexKo set the quitOnLastWindowClosed property to false It works, thank you!
  • How to get detailed fail messages when using ifstream.fail() in C++

    Moved Unsolved c++ ifstearm messages fail
    4
    0 Votes
    4 Posts
    10k Views
    J
    In C++, you can use the std::ifstream::fail() function to check if there was a failure during file operations. However, to get more detailed error messages, you can use the std::ifstream::rdstate() function in combination with std::strerror() or std::perror(). Here's an updated version of your code with more detailed error messages: #include <iostream> #include <fstream> #include <cstring> void open_file(std::ifstream& ifstream, const std::string& file_name) { ifstream.open(file_name); if (ifstream.fail()) { // Get the error code std::ios_base::iostate state = ifstream.rdstate(); // Check for specific error bits if (state & std::ios_base::eofbit) { std::cout << "End of file reached." << std::endl; } if (state & std::ios_base::failbit) { std::cout << "Non-fatal I/O error occurred." << std::endl; } if (state & std::ios_base::badbit) { std::cout << "Fatal I/O error occurred." << std::endl; } // Print system error message std::perror("Error: "); return; } std::cout << "File was opened successfully!" << std::endl; } In this code, we use std::ios_base::iostate to retrieve the error state of the ifstream object. Then, we check specific error bits like std::ios_base::eofbit, std::ios_base::failbit, and std::ios_base::badbit to provide more detailed information about the failure. The std::perror() function is used to print the system error message corresponding to the error code. This will give you additional information about what went wrong. Remember to include the necessary headers for this code to work: #include <iostream> #include <fstream> #include <cstring>