Given below is the code for embedding an application using QX11EmbedContainer.
#include "mainwindow.h"
#include <QApplication>
#include <QX11EmbedContainer>
#include <QProcess>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QX11EmbedContainer container;
container.show();
QProcess process(&container);
process.start("C:/Users/Administrator/Desktop.../.exe");
int status=a.exec();
process.close();
return status;
}
On running the application the error that I am getting is:
C:\Qt4.8.5\src\gui\kernel\qx11embed_x11.h:77: error: C2061: syntax error : identifier 'XEvent'
Where am I going wrong?
QX11EmbedContainer is X11-specific, it will not work under other systems (like Windows).
Related
I want to use std::variant<Foo, Bar> as internal value of QVariant. How to do this?
#include <QCoreApplication>
#include <QMetaType>
#include <variant>
enum Foo{A, B};
enum class Bar{C, D};
Q_DECLARE_METATYPE(std::variant<Foo, Bar>);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
I am getting this error:
error: macro "Q_DECLARE_METATYPE" passed 2 arguments, but takes just 1
Q_DECLARE_METATYPE(std::variant<Foo, Bar>);
^
The problem is Q_DECLARE_METATYPE is a macro, and macros don't like ,s.
You can either pass a type alias
using T = std::variant<Foo, Bar>;
Q_DECLARE_METATYPE(T)
or wrap the use in extra ()
Q_DECLARE_METATYPE((std::variant<Foo, Bar>));
I am busy with my university assignment, i'm new to Qt, C++ and i am required to:
Define and implement the Address class in separate header and source files
I am getting confused with Qt4 and Qt5 as the prescribed text book gives all examples in Qt4 yet we are required to code with Qt5.
I keep getting errors and the latest error is :
error: undefined reference to
Dialog7getTextEP7QWidgetRK7QStringS4_N9QLineEdit8EchoModeES4_
Pb6QFlagsIN2Qt10WindowTypeEES8_INS9_15InputMethodHintEE'
error: undefined reference to
MessageBox11informationEP7QWidgetRK7QStringS4_6QFlagsINS_
14StandardButtonEES6
collect2.exe:-1: error: error: ld returned 1 exit status
I am very confused and stuck, please if anyone can steer me in the right direction i would greatly appreciate it, This is my code so far:
Header File:
#ifndef ADDRESS_H_
#define ADDRESS_H_
#include <QString>
#include <QFile>
#include <QStringList>
#include <QtCore>
QTextStream cout(stdout);
QTextStream cerr(stderr);
class Address{
public:
Address();
void setLines(QString ad, QString sep);
QString getPostalCode() const;
QString toString(QString sep) const;
static bool isPostalCode(QString pc);
private:
static QStringList lines;
};
#endif // ADDRESS_H_
Main File:
#include "address.h"
#include <iostream>
#include <QFile>
#include <sstream>
#include <fstream>
#include <QStringList>
#include <QString>
#include <QTextStream>
#include <QCoreApplication>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QLineEdit>
Address::Address(){}
void Address::setLines(QString ad, QString sep){
QStringList lines;
lines = ad.split(sep, QString::SkipEmptyParts);
}
QString Address::getPostalCode() const{
QStringList lines;
return lines.last();
}
QString Address::toString(QString sep) const{
QStringList lines;
return lines.join(sep);
}
bool Address::isPostalCode(QString pc){
if (pc >= "0" && pc <= "9999")
return true;
else
return false;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while(true)
{
QString userInput = QInputDialog::getText(0,"Address Input","Please
enter your address:");
QLineEdit::EchoMode ok = QLineEdit::Normal;
QString();
if(ok && !userInput.isEmpty())
{
Address ad;
QMessageBox::information(0, "User Address",ad.toString(userInput));
}
}
return 0;
}
I got the same error. Basically, there's an issue with your compiler MingW and qMake. The best suggestion I have is to uninstall QTcreator and everything that references it. And re-install it using the new version: https://www.qt.io/download
This will install everything you need and all the right directories. Hope this helps.
I am starting to learn threads in the C++11 standard in Qt.I can't include library ,no such of directory For example, I have the following simple code:
#include <QCoreApplication>
#include <thread>
#include <iostream>
using namespace std;
void func();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread th1;
th1.run();
return a.exec();
}
void func()
{
cout << "This string from thread!"<<endl;
}
On the second string of the code, I have an error. The compiler doesn't "see" , I know that i must "include" 11 standard, so i go to .pro and paste CONFIG += c++11, but it isn't helping me :C
Please, I need your help!
You try to use QThread subclass, but you said that you want to use C++11 thread so use this:
#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>
void foo()
{
std::cout << "This string from thread!"<<endl;
//while(true)
//{
// qDebug() <<"works";
// Sleep(500);
//}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::thread t(foo);
t.join();
return a.exec();
}
Also next is wrong:
MyThread th1;
th1.run();
Should be:
MyThread th1;
th1.start();
Details about threading with Qt classes:
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
http://qt-project.org/doc/qt-5/thread-basics.html
http://qt-project.org/doc/qt-5/qtconcurrent-index.html
I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.
i'm trying to learn c++, but i can not find if it's possible to extend a class in this way:
main.cc
#include "mWindow.h"
using namespace std;
int main( int argc, char* argv[] ) {
gtk_init( &argc, &argv );
mWindow win = mWindow();
gtk_main();
return 0;
}
mWindow.cc
#include "mWindow.h"
mWindow::mWindow() {
gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (this, "my window");
gtk_widget_show_all (GTK_WIDGET(this));
}
mWindow.h
#ifndef MWINDOW_H_INCLUDED
#define MWINDOW_H_INCLUDED
#include <gtk/gtk.h>
using namespace std;
class mWindow : public GtkWindow {
public:
mWindow();
};
#endif
I suggest you take a look at gtkmm (http://www.gtkmm.org/) if you want to use GTK+ in conjunction with C++, i.e. there is no need to try to reinvent the wheel and write your own C++ interface for GTK+ (which is a C library).
thanks,
I was trying to use C libraries as if they were C++.
This is how I solved with gtkmm:
main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window);
return 0;
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm-2.4/gtkmm.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
show_all_children();
}
also add the command to complete successfully, at least on Arch Linux:
g++ $(pkg-config --cflags --libs gtkmm-2.4) main.cc examplewindow.cc examplewindow.h -o executable
another small indication, what i shouldl use as dynamic arrays or vectors and for hashmap?