QT : get the class name of an object - unit-testing

I'm writing a test app that simulates key presses of another application. For every key press I have to check if the right window/form is shown. So what I do is get the pointer of the window being shown and get it's window title. However, not all the windows/forms shown window titles. So I'm thinking it would be better to get the name of the class instead. How can I get the name of the class?
QWidget *pWin = QApplication::activeWindow();
when I try:
pWin->className();
to get the name of the class, I'm getting:
"error: class QWidget has no member named 'className' "
Can somebody show me the right way?

Try using the metaobject.
pWin->metaObject()->className();

You could also check the typeinfo header. Using the typeid operator on you object you get a type_info instance which describes the type of your object.
Check out: http://www.cplusplus.com/reference/std/typeinfo/type_info/

Related

Referencing QWidget from different class

I'm sorry if this question is duplicate but i am really struggling with finding any answer.
Please take in mind that i am novice in c++ programmming.
My problem is this. I have an GUI made in QtCreator. There are two listeners binding keyReleaseEvent, one on main class (SuperFalcon) , one on QTextEdit ( which is separate and modified class ). I have QFrame which i would like to toggle hide/show on "Ctrl + f" key event. Since that QFrame (object name is findWidget) widget belongs to SuperFalcon->ui, there's no problem, everything works fine, problem starts when i try to make "Ctrl + f" in QTextEdit because it's separate event listener. Basically i tried this.
main class name is "SuperFalcon" so:
in superfalcon.h i've made an public static pointer like this:
public:
static QFrame *fWidget;
then in superfalcon.cpp, i firstly execute
ui->findWidget->hide(); and then
fWidget = ui->findWidget; hoping to get pointer on widget.
Next in my QTextEdit class in keyReleaseEvent function i've tried to get that pointer like SuperFalcon::fWidget->show() but i get undefined reference on it.
So , to make things simpler, i don't know how , if possible, to get reference of QFrame widget which is part of one class (SuperFalcon), from another class (QTextEdit class) in order to execute some commands on QFrame.
If it's not clear enough i can provide some code.
You must have a definition of any static member variable.
This definition has to be in a source file because of the one definition rule.
Simply add the line:
QFrame* SuperFalcon::fWidget;
to "superfalcon.cpp".
You have to initialize your static variable, in superfalcon.cpp:
QFrame* SuperFalcon::fWigdet = nullptr;

How to get the text value of a control through its parent window

I have the following wxDialog parent window:
I have created that parent window by the following code:
settingsFrm settingsWindow(this, "Settings");
settingsWindow.ShowModal();
I have tried to use FindWindowByName to get the value of the first text ctrl as follow:
wxLogMessage(dynamic_cast<wxTextCtrl*>(settingsWindow->FindWindowByName("keywords_txt"))->GetValue());
But unfortunately, it doesn't work and gives me a runtime error.
I don't know if that method suitable to do what I want.
How to get the value/other of a control through its parent window?
From your comments, it seems like you expect the function to find the control from the name of the variable in your code which is not how it works and would be pretty much impossible.
FindWindowByName() uses the window name (and, as a fallback, label, but this is irrelevant here because text controls don't have labels anyhow), so for it to work you need to set the window name when creating the control, using the corresponding parameter of its ctor. This is very rarely useful in C++ code, however, as it's simpler to just store a pointer to the control in some variable and use this instead.
FindWindowByName() can often be useful when creating controls from XRC, however. If you do this, then you should specify names for your controls in XRC and pass the same name to this function.
How did you create the TextCtrl instance? You should have something like wxTextCtrl m_textCtrl1 = new wxTextCtrl(/// arguments); Accessing the value should be very easy, as wxString text = m_textCtrl1->GetValue(); You definitely don't need FindWindowByName just for what you are trying to do here.

How to set the frame shape of a QTextEdit

I just cannot grasp the enum in class concept syntactically.
I am trying to disable QTextEdit's frame:
//in a header for my custom class where the main element is the textField
QTextEdit* textField;
...
//displaying it myCustomClass.cpp
textField = new QTextEdit(this);
textField->Shape = QFrame::NoFrame;
I get the error "invalid use of enum::Qframe::Shape". What is the correct syntax and why?
That's invalid C++: there's no such "Shape" member for QTextEdit. Moreover, Qt uses proper encapsulation, so the shape is not exposed by a member variable.
You have to call a method which sets the frame shape, and surprisingly enough, it's called setFrameShape!
textField->setFrameShape(QFrame::NoFrame);

cant get a pointer to wxwidget object, using wxsmith

straight to the point:
Im learning wxsmith and wxwidgets toolkit, i created some basic GUI containing one button and 2 static text fields. GUI is compilling ok so far. My frame name is proba2Frame, then im adding my own function which is not a member of any class but i declared in header file for proba2Frame that my function is a friend. Below is code of my function:
wxStaticText * dawajpointera()
{
wxStaticText * text;
text = proba2Frame.wxStaticText.StaticText1;
return text;
}
im getting error:
expected primary-expression before ‘.’ token
What exactly im doing wrong and how to get a pointer StaticText in case my solution is completely wrong ?
Thank You in advance
You make it sound like proba2Frame is the name of a class inheriting wxFrame?
If so, you're haveing problems because you haven't created an instance of proba2Frame, and you're trying to access a part of it that hasn't been constructed. Your main frame class is simply a template for your GUI, not the GUI itself.
The best way to go about it would probably be to take an instance of proba2Frame as a parameter-
wxStaticText* dawajpointera(proba2Frame *frame)
{
return frame->StaticText1;
}
Of course, that function itself was a bit pointless, but I'll assume that you're going to do something more involved with the pointer afterwards, and want it set to a pointer named text within the function for the sake of brevity.
void func(proba2Frame *frame)
{
wxStaticText *text = frame->StaticText1;
// Do something with text
}
If you're doing this, though, please consider making the function a method of proba2Frame.
wxStaticText is the name of a wxWidgets class. You should not be naming attributes of your frame 'wxStaticText'. Despite the code you have posted, I doubt that you have really done such a terrible thing. What you probably meant to write, I would guess, is:
text = proba2Frame.StaticText1;
I am guessing that the name of the attribute is StaticText1, a pointer to an instance of the wxStaticText class.

Passing QSqlQueryModel through control class

How would I go about passing QSqlQueryModel from a class that connects and queries the database through the control class or QMainWindow in my attempt and back to the widget needing the information?
I thought I could pass the reference location to the QSqlQueryModel object, but this is not working or I am doing something wrong.
I haven't found any examples showing what I am doing on the Qt Developer page.
Looks like these are just compiler errors, nothing specifically to do with Qt.
In short you are getting your pointers and references mixed up.
Error #1:
cardList = new List(sqlModel->getListModel());
You are passing a reference when the List takes a pointer. Fix your return type from getListModel or fix the above line.
Next, you are not specifying the second argument, i.e. the parent QWidget. Either specify your MainWindow as the parent, pass 0, or fix your constructor's signature to provide a default (generally 0).
Error #2:
List::List(QSqlQueryModel *model, QWidget *parent) : ListUI(parent){
setListItems(&model);
}
You receive the model as a pointer and then attempted to take the address of the pointer. I.e. You're making a double pointer. Change the line to
setListItems(model);
Hope that helps.