Skip to content

C++ Gurus

The forum for all discussions in C++ land.
1.3k Topics 8.5k Posts
  • 0 Votes
    13 Posts
    9k Views
    T

    That all depends on how your file is encoded... you need to decode it properly using the right text codec.

  • C++ global class varible definition

    10
    0 Votes
    10 Posts
    11k Views
    L

    [quote author="Simon6Sweet" date="1309760515"]Is this the right code for instance? @ class user { private: int id; static int next_id; public: static int next_user_id() { next_id++; return next_id; } /* stuff for the class */ user() { id = user::next_id++; //or, id = user.next_user_id(); } }; int user::next_id = 0; @ [EDIT: code formatting, Volker][/quote]
    Almost. @
    id = user::next_id++;
    @
    is not the same as
    @
    id = user.next_user_id();
    @
    which would be the same as
    @
    id = ++user::next_id;
    @
    See "here":http://www.imb-jena.de/~gmueller/kurse/c_c++/c_operid.html for an explanation.

  • 2D Drawing

    3
    0 Votes
    3 Posts
    6k Views
    G

    [quote author="srikanthsombhatla" date="1309362551"]
    Coming to the warning, you are using QPainter wrongly. Qt provides a QPainter with in the paint context or when a paint event is triggered, using which you should draw your widget. So which QPainter are you using in void SimpleExampleWidget::updateT(). That is not the painter which Qt prepared for you so that you can paint on your widget. Atleast try to pass the painter as an argument to the method in which you are painting.[/quote]

    This is only partly correct. QPainter is always initialized by the application.

    But you should use it inside paintEvent if you draw on the widget. This is, because Qt uses double buffer technologies behind the scenes and manages all this double buffering, invalidation, clipping etc.

    so the correct way would be to trigger an update or repaint after updating the position. And inside the paint event, draw whatever you want. Or create a transparent label or custom widget and put the object in there. Then you can move the object by timer or animation framework.

  • Threading in Linux and MacOS

    5
    0 Votes
    5 Posts
    3k Views
    W

    QVector is not threadsafe, and neither is std::vector. If you have a shared resource, accesses to it must be serialized.

    Another question, why are you using a pointer to a float rather than just a float in your vector? You are not using any less memory.

  • Plugging-in Audio File

    2
    0 Votes
    2 Posts
    2k Views
    E

    "i was looking on this thread but there was no concrete answer.. one person also said its a problem in new sdk":http://developer.qt.nokia.com/forums/viewthread/4235

  • Pointer usage in Qt

    7
    0 Votes
    7 Posts
    5k Views
    F

    [quote author="quasimodo" date="1308432868"]Children are deleted by the parent really acceptable but who deletes the parent?
    What if I take care of the order of classes?[/quote]It's irrelevant who deletes the parent, as long as someone deletes the parent. The parent is usually the one object that can be created on the stack, simply because there is no object deleting it.

    [quote author="quasimodo" date="1308432868"]If you want to use heap which we call it free store in C++, you should use smart pointers like auto_ptr or other ones implemented by boost or Qt's ones which is told by @leon.anavi. That's a kind of pointer usage is not acceptable in standart C++ because of exception handing mechanism.
    [/quote]

    C++ is a free form, "to each their own" type of language. The fact that references and templates have been added to allow you to write safer, more readable code doesn't mean that you have to use them. Nor does it mean that they will actually help you to write safer and more readable code. Books can teach you a lot, but in my experience, practice always comes with cases that books don't really handle.

    Writing exception safe code is a task for the programmer. In most cases, developers will pass on the C++ exception handling, simply because it's considered tricky at best. Ah well, let's not get into that. For me personally, using smart pointers without exception for all heap allocated objects in not acceptable. It's often creating more code bloat than it solves, but again, to each their own.

  • How to handle dynamic memory allocation failure

    35
    0 Votes
    35 Posts
    25k Views
    L

    [quote author="Meraj Ahmad Ansari" date="1307953504"]
    Frankly speaking I never considered it as a question. I really want to know What type of technique other developers are using.[/quote]

    As I think this is directly related to your last question I'll answer right here.

    To me, there are usually two types of heap allocations in an application:

    small, but frequent allocations required for the program to flow, like creating widgets or objects on the heap for the user interface or eg. network processing large, but infrequent allocations required for some specific tasks and not necessarily required for the program to flow, like a buffer for loading eg. a large image into memory which has been selected by the user in an image processing application

    The way you react to out of memory situations will differ for both types.

    I see no way a program can or should recover from such situations. It should save any data which has to be saved to a persistent storage and then exit. For this task, you will usually need just a limited amount of information, which then has - of course - to be available to your new handler / exception handler. This allows you to handle out of memory situations at a single point in your code.

    Usually the required information is available through your "design" anyways, because you'll need it for your normal program flow too (eg. Application::instance()->openedImage()->data() to stay at our image processing application example). There is no need to access all the other data not relevant for you recovery code, especially there is no need to delete anything because your application gets aborted anyways.

    However, if you need to allocate further memory in your recovery code you should have reserved some already (the "emergency heap" discussed earlier).

    There is no need to abort the application in such situations. Allocations failures can be caught using std::nothrow and a null pointer check or local exception handling.

    Probably my last post was a bit misleading. Of course there are situations where it is absolutely legitimate to use a std::nothrow / null pointer check, but you should not guard every single allocation with it.

  • Show UI Design not in main metod is possible?

    17
    0 Votes
    17 Posts
    6k Views
    G

    The sample is in QSplashScreen's documentation... have a look over there.

    You are not required to write a processEvents() method, but to use the provided one...

  • It is a bug?

    3
    0 Votes
    3 Posts
    2k Views
    P

    Ok thank you, I got it now.

    Best regards

  • Implementing pointers to methods

    3
    0 Votes
    3 Posts
    2k Views
    E

    Thanks Franzk

    I wasn't certain if signals & slots could be used in this way but it turns out it works beautifully.

    Somehow slots can be communicated as regular character strings and I don't know how it REALLY works but they can passed on as a parameter this way to be connected to elsewhere and that's all I really care about :)

    I'm really starting to like Qt :D

  • How to catch unhanded exception ?

    7
    0 Votes
    7 Posts
    6k Views
    J

    I use the Windows ::SetUnhandledExceptionFilter API:

    @
    ....
    ::SetUnhandledExceptionFilter( IGExceptionHandler );
    ...

    #if defined(WIN32)
    LONG WINAPI IGExceptionHandler( struct _EXCEPTION_POINTERS pExceptionInfo )
    {
    DWORD code = pExceptionInfo->ExceptionRecord->ExceptionCode;
    IGLog( qDebug(), "Handling Visimeet Exception : " << code << " Address: " << pExceptionInfo->ExceptionRecord->ExceptionAddress);
    if ( code == STATUS_ACCESS_VIOLATION )
    { // add this bit o' info
    IGLog( qDebug(), "*** ACCESS VIOLATION ")
    }
    else if ( code == STATUS_ILLEGAL_INSTRUCTION )
    {
    IGLog( qDebug(), "
    ILLEGAL INSTRUCTION ");
    }
    else if ( code == STATUS_FLOAT_DIVIDE_BY_ZERO || code == STATUS_INTEGER_DIVIDE_BY_ZERO )
    {
    IGLog( qDebug(), "
    DIVIDE BY ZERO ");
    }
    else if ( code == IGLOGGER_FATAL_ERROR )
    { // our interanl error if qFatal is used
    IGLog( qDebug(), "
    Qt FATAL ERROR ")
    }
    else
    {
    IGLog( qDebug(), "
    Unknown Fatal Error ****");
    }

    LONG retval = EXCEPTION_EXECUTE_HANDLER; visiqt->sendReport(true); return retval;

    }
    @

  • 0 Votes
    7 Posts
    5k Views
    G

    Can you provide the complete file that produces the error, please. It's hard to analyze without knowing the details.

    The second try is just leon's without acutally defining the variable in a .cpp file. So it's failing at compile time.

    The third approach (leon's suggestion) is trapped by the undefined initialization order of static members (the QIcon is initializize - via a QPixmap - before QApplication has started), that's the reason for the bail out.

    I would suggest to make a private static pointer variable; initialize this to null (that works always), and have a static const method return the dereferenced pointer (and initialize it once).

    The following sample code is not tested, but should give you a clue how it works:

    -------- myiconproviderclass.h --------
    class QIcon;

    @
    class MyIconProviderClass {
    public:
    // your other public stuff goes here
    static const QIcon &myIcon();

    private: static QIcon *myIconPtr;

    @

    -------- myiconproviderclass.cpp --------

    @
    #include "myiconproviderclass.h"
    #include <QIcon>

    QIcon* MyIconProviderClass::myIconPtr = 0;

    const QIcon& MyIconProviderClass::myIcon()
    {
    if(!myIconPtr)
    myIconPtr = new QIcon(":/path/to/the/icon");

    return *myIconPtr;

    }
    @

  • Callback function

    4
    0 Votes
    4 Posts
    7k Views
    F

    [quote author="cincirin" date="1306334700"]If your static class member function match the declaration of your C library callback, then there should be no problem in your code.[/quote]

    As stated in the cpp faq, this includes the calling convention (cdecl, stdcall, that sorta thing).

  • The Singleton Pattern

    15
    0 Votes
    15 Posts
    18k Views
    G

    See "C++ FAQ 16.8":http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.8:

    bq. C++ language guarantees that delete p will do nothing if p is equal to NULL. Since you might get the test backwards, and since most testing methodologies force you to explicitly test every branch point, you should not put in the redundant if test.

    It is guaranteed by the standard that assigning 0 (digit zero) to a pointer in the source code is equivalent to assigning null to the pointer (the same holds for comparison with ==).

    This is independent of the internal representation of a null pointer! The latter can be different from the numeric value zero, so memsetting all bits of a pointer to zero can lead to something different than a null pointer!

  • 0 Votes
    3 Posts
    3k Views
    A

    Thank you for sharing!

  • Displaying image using label in Qt4.7

    6
    0 Votes
    6 Posts
    14k Views
    J

    Thanks for yours response. I just found my mistake. I should have put clicked() instead of triggered().

    Thanks a lot

  • Need to swap row in a QTableWidget

    3
    0 Votes
    3 Posts
    7k Views
    P

    Thanks a lot, I use you model function to take the model, and I sort a column in this model. So the column represent the index and I just need to swap the index value in column and I use sort function.

  • Function pointers in Qt

    30
    0 Votes
    30 Posts
    26k Views
    G

    Good night :-)
    Please regard the updates I made to the code

  • My sub-sub-class is not being included

    15
    0 Votes
    15 Posts
    5k Views
    A

    For Linux users:
    I kept on having problems when attempting to run the application built in QtCreator, I tried the -R option but it didn't work. In the end I found out somewhere on internet that I should create a myLibs.conf file inside /etc/ld.so.conf.d with the path to my libraries. That did the trick, fyi.

    Boris

  • 0 Votes
    6 Posts
    4k Views
    K

    Thanks for clarification. The naming is a little misleading then. That made me stumbling when trying to understand. Anyhow, I am happy that your problem has been solved.