Archiwum kategorii: In English

Examples of QString::number

Some examples of a very useful Qt function: QString::number. This function is helpful in converting double/float to string. QString, to be more precise 😉

QString::number has got 3 parameters:

  • double n – number, which will be converted
  • char format = ‚g – format: available formats are f, e, E, g, G. Default is ‚g’:
  • int precision = 6 – precision.

Let me show some examples. On the end of each line is a output from console:


//f - float
QString::number(3.14159265359, 'f', 0); //3
QString::number(3.14159265359, 'f', 1); //3.1
QString::number(3.14159265359, 'f', 2); //3.14
QString::number(3.14159265359, 'f', 15);//3.141592653590000

//e - exponential notation
QString::number(3.14159265359, 'e', 15); //3.141592653590000e+00
QString::number(3.14159265359, 'E', 15); //3.141592653590000E+00

//g/G - "general" - in this case: "f"
QString::number(3.14159265359, 'g', 0); //3
QString::number(3.14159265359, 'g', 1); //3
QString::number(3.14159265359, 'g', 2); //3.1
QString::number(3.14159265359, 'g', 15); //3.14159265359

QString::number(3.14159265359, 'G', 2); //3.1
QString::number(3.14159265359, 'G', 15); //3.14159265359

//g/G - "general" - in this case: "e/E"
QString::number(2.5e+5, 'g', 0); //2e+05
QString::number(2.5e+5, 'g', 1); //2e+05
QString::number(2.5e+5, 'g', 2); //2.5e+05
QString::number(2.5e+5, 'g', 15); //250000

QString::number(2.5e+5, 'G', 2); //2.5E+05
QString::number(2.5e+5, 'G', 15); //250000

//comparision with f and e/E:
QString::number(2.5e+5, 'f', 2); //250000.00

QString::number(2.5e+5, 'e', 15); //2.500000000000000e+05
QString::number(2.5e+5, 'E', 2); //2.50E+05

How to run tests in Visual Studio?

How to run tests (unit tests, Selenium or any other in Microsoft Visual Studio? This is quite simple.

My instruction is for Visual Studio Professional 2015.

  1. First – choose test runner (nUnit, MSTest, xUnit or other). I choosed nUnit, because of its popularity.
  2. Install a test runner. I use NuGet to manage dependencies in my projects. To install nUnit, click (right) on project and click „Manage NuGet Packages”, search „nUnit” and install it. A current version number is 3.4.1.
  3. Prepare a simple test. Remember about:
    • making class containing tests public (public class TestClass{})
    • making test methods public (public void TestMethod(){})
    • marking test methodss with [Test] attribute.
  4. Prepared test should be visible in Test Explorer (open this window from menu: Test -> Windows -> Test Explorer). If Test Explorer window is empty and after building project Visual Studio doesn’t display any tests, you need to install nUnit Test Adapter (version 3.x is compatible with nUnit 3.x). To install nUnit Test Adapter, choose „Extensions and Updates” from „Tools” menu, search for „nUnit Test Adapter” and install it. After rebooting Visual Studio and rebuilding project, tests should be displayed in Test Explorer Window.

How to install LHAPDF6

Instruction is easy to find here: https://lhapdf.hepforge.org/install.html. But on „fresh” Linux you need to install some dependencies.

Download LHAPDF archive from page https://www.hepforge.org/downloads/lhapdf and unpack to any directory. Alternative method is to use wget:

wget http://www.hepforge.org/archive/lhapdf/LHAPDF-6.6.1.6.tar.gz
tar xf LHAPDF-6.1.6.tar.gz
cd LHAPDF-6.1.6

Installation is standard: configure, make, make install.

I prefer to not use any special path and install this lib directly to /usr/local/lib. So I didn’t use any prefixes:

./configure

After invoking this command I got a message:

checking for Boost headers version >= 1.41.0... no
configure: error: cannot find Boost headers version >= 1.41.0

Boost was missing. To install:

sudo apt-get install libboost1.48-*

When installation process ends, checking if boost is installed:

dpkg -s libboost1.48-all-dev

How to get version of Boost:

dpkg -s libboost1.48-all-dev | grep 'Version'

In my case: Version: 1.48.0-3.

After installing Boost, retry configure LHAPDF:

./configure

Succeed. Installation:

make
sudo make install

How to check if LHAPDF is installed? Try to invoke:

ls /usr/local/lib/

How to check if element is invisible in Ruby Selenium client

How to check if element is invisible in Ruby Selenium client? In Java or C# answer is easy. There is a mechanism called ExpectedCondition, which has got ready for use method: ExpectedConditions.invisibilityOfElementLocated. Example:

driverWait.until(ExpectedConditions.invisibilityOfElementLocated(element));

Unfortunatelly, Ruby client hasn’t got anything like this. Quick workaround – try to find element. When element is not displayed, catch exception and return false:

def check_if_not_exist(locator)
  begin
    wait = Selenium::WebDriver::Wait.new :timeout => 10
    wait.until { @driver.find_element(:name, 'elementName').displayed? }
  rescue
    false
  end
end

Not very elegant and quick, but works.

 

Selenium: Exception „Compound class names are not supported”

Today something about one of exceptions that can occure in Selenium PageFactory.

IllegalLocatorException was unhadled by user code.
An exception of type „OpenQA.Selenium.IllegalLocatorException” occured in WebDriver.dll but was not handled in user code.
Additional information: Compound class names are not supported. Consider searching for one class name and filtering the results.

In my case, this exception was thrown in line:

PageFactory.InitElements(webDriver, pageElement);

This is probably a bug in Selenium. Solution is easy: just do not use:

[FindsBy(How=How.ClassName, Using="name_of_class"]

Instead of ClassName, try to use Css Selectors:

[FindsBy(How=How.CssSelector, Using=".name_of_class"]

Remember about a dot!

Ruby: hash as argument

What will Ruby do, when we pass hash as argument to function? Let’s see.

We have method – with definied default value (empty hash):

def foo(hashh = {})
  puts 'Is hash empty? ' + hashh.empty?.to_s,""
end

1. When we invoke function with any hash, it will be passed into method. Output? „Is hash empty? false„.

foo({:id => 'someid'})

h = {:id => 'first', :name => 'noname'}
foo(h)

2. When we invoke function without any parameter, Ruby will take default value – empty hash. Output is „Is hash empty? true„.

foo

3. Of course, when we pass nil to function, we will got exception: „undefinied method ’empty’ for nil:NilClass„.

foo(nil)

How to debug remote application on Tomcat

Some time ago I wrote about a situation when starting of Tomcat from Netbeans failed. Of course when someone follows with steps prepared in this article, debugging is more complicated. But there is one nice feature in Tomcat – we can debug remote servers.

First thing to do is to enable debugging applications deployed on Tomcat, start Tomcat by this way:

Tomcat/bin/catalina.bat jpda start

By default, Tomcat is listening on port 8000 (changing default port is possible in some configuration files).

In second step we should connect from Netbeans to our application. Sources in IDE (in my case: Netbeans) must be identical with classes deployed on Tomcat! If there are any differences, some breakpoints will be unavailable and debugging will not working properly.

Go to Debug menu and fill forms:

Debugger: Java Debugger (JPDA)
Connector: SocketAttach
Host: 127.0.0.1
Port: 8000

and click OK. After a while, breakpoints should be available. Look into console. There are info about enabling each breakpoint.

Starting of Tomcat failed

Some day I had problem with deploying project from Netbeans to Tomcat (you may look to similar discussion on StackOverflow: Starting of Tomcat failed from Netbeans 7.3). Unfortunatelly, any of suggestion didn’t help, so I wrote my own deployer in Windows batch.

Some code:


ECHO AutoDeploy to Tomcat

SET tomcat_path=C:\TOMCAT
SET build_path=D:\SVN\SMOK_ELTE\JAVA\amfgw\build\web\WEB-INF\classes

::Stop Tomcat
echo Stop Tomcat, press any key to continue...
pause

netstat -a -n | findstr :8080 > NUL
if %errorlevel%==0 ( goto :stoptomcat ) else ( goto :next )
:stoptomcat
echo Closing Tomcat.
call %tomcat_path%\bin\shutdown.bat
goto :next
:next

::Copy compiled classes from Netbeans to Tomcat
XCOPY %build_path% "%tomcat_path%\webapps\ROOT\WEB-INF\classes" /E /C /R /I /K /Y

::Run Tomcat
CALL %tomcat_path%\bin\catalina.bat jpda start

But I wanted more – let Netbeans run this script after building my application.

Edit build.xml file, located in project directory, by putting these lines:
<target name="-post-compile">
<exec dir="." executable="cmd">
<arg line="/c D:\scriptLocation\deployOnTomcat.bat"/>
</exec>
</target>

Netbeans doesn’t start

Sometimes after installation new plugins to Netbeans, if plugins are not compatible, Netbeans can’t run.  Unfortunatelly, deleting these new files from Netbeans directory didn’t helps. So what can we do?

One thing is to open configuration file and disable new plugin.

In Windows7, go to location:

{drive}:\Users\{yourUserName}\AppData\Roaming\NetBeans\{versionNumber}\config\Modules

and find this file, for example:

org-netbeans-modules-websvc-axis2.xml

Try to disable this plugin by putting „false” in tags:

<param name="autoload">false</param>
<param name="eager">false</param>
<param name="enabled">false</param>

If disabling plugins didn’t helps, try to delete all Netbeans configuration by deleting directories (before deleting, you can make a copy of these files):

{drive}:\Users\{yourUserName}\AppData\Roaming\NetBeans\{versionNumber}\
{drive}:\Users\{yourUserName}\AppData\Local\NetBeans\Cache\{versionNumber}\

Unfortunatelly, this operation removes all configurations (servers, loaded projects and other).